PDA

View Full Version : select() in a form - javascript



susie123
10-17-2007, 04:11 PM
Hi. I have written the following text which works totally except for one thing. If the email input box has been typed into, after the message "Please enter a valid email address"l, I want that text written in error to be highlighted so the client doesn´t have to delete it and can just type straight back in with a corrected email address.

function email_onblur()
{
var email = document.form1.email.value;
var atSign = email.indexOf("@")
if ((atSign == -1) == true)
{
alert("Please enter a valid email address.");
myForm.email.focus();
myForm.email.select();

}
}

I feel it is the text highlighted in green above that is not working, everything else is ok. Any ideas?

Many thanks.

jscheuer1
10-17-2007, 04:49 PM
If:


myForm.email

refers to the proper element, then after focusing on it:


myForm.email.select()

will select it. However, you may have the wrong element, or no element.

In an effort to eliminate those possibilities, try:


function email_onblur(){
var e = document.form1.email;
var atSign = e.value.indexOf("@")
if (atSign < 0){
alert("Please enter a valid email address.");
e.focus();
e.select();
}
}

Also, the use of an alert my be overriding the focus() and/or select() directives - or just be changing the focus and selection of the document after those directives. Remember, you have to click on an alert to close it.

If you need more help:

Please post a link to the page on your site that contains the problematic script so we can check it out.