Domanda

I have an email page that loads up when I click on a button. The 'To:' field is containing too many emails and I would like to run a script for when the page loads to check if the box contains a semi-colon and if it does, it should remove the semi-colon and any text after it.

This is the text area's HTML code when I view the script on the page:

<textarea class="EDIT" name="email_to" id="email_to" maxlength="8000" rows="1" style="height:2em;" cols="80">test@testemail.com;test2@testemail.com;</textarea>

So when the page loads the script should check the email_to field. For example:

[test@testemail.com;test2@testemail.com]

should have its semi-colon removed as well as anything after it to leave this:

[test@testemail.com]

What I have so far is as follows

function checkTextField(email_to) {
    var check = (email_to.indexOf(';');
    if (check != -1)  
        {
        check.replace(/;.*/, '');
        }
}

My main problem I think is that when I do a console.log(email_to) it gives me all of styles and everything for the textbox, and all I want is the text held in the textarea.

It may not need to be in a function and could just be an onload script but I am new to javascript so I am not sure what to use.

È stato utile?

Soluzione 3

So after a bit more searching I found the way I was doing it was unnecessary, and was told to use the following:

function checkTextField (str) {

    return str.substring(0, str.indexOf(';'));
}

$('#email_to').val(checkTextField($('#email_to').val()));

This seems to work perfectly, and it looks like I need to brush up on jQuery also!

Altri suggerimenti

To my understanding you want to be operating on "email_to.value", not the "email_to" textarea itself

Take only the html, not the whole element.

 function checkTextField(email_to) {
    var ht = email_to.innerHTML;
    var check = (ht.indexOf(";");
    if (check != -1)
    {
       // Bla bla
    }
 }

Note: The function is copied from yours, I only added what you asked for, you have more problems there.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top