Click to See Complete Forum and Search --> : string manimpulation/validation + javascript


peach255
01-31-2003, 08:42 AM
I am lost on this. Please help. I want to validate the text field ("manemail") has "viropharma.com" at the end. If not, an alert will pop up.
I know I have this code using "substring" wrong. Please help.

checkEmail = form.manemail.value

if ((checkEmail.substring('xyz.com') =0)
{alert("Need xyz.com");
form.manemail.select();
return false;
}

Thanks!

khalidali63
01-31-2003, 08:46 AM
checkEmail = form.manemail.value

if ((checkEmail.substring('xyz.com') =0)
{alert("Need xyz.com");
form.manemail.select();
return false;
}


set the condition to >-1 instead of =0
if ((checkEmail.substring('xyz.com') >-1)


cheers

Khalid

peach255
01-31-2003, 09:12 AM
thanks for your help, but this did not work:
if ((checkEmail.substring('xyz.com') >-1)

I thought for substring, you need substring(from,to), and from and to are # positions.

Is there another way I can easily do this?

thanks!

Charles
01-31-2003, 09:43 AM
Originally posted by peach255
I thought for substring, you need substring(from,to), and from and to are # positions.No, that's optional. See http://developer.netscape.com/docs/manuals/js/client/jsref/string.htm#1194665.

Another way to do this would be to use:

<font action="someScript.pl" onsubmit="if (!/xyz\.com$/.test(this.manemail.value)) {alert('Need \\'xyz.com\\'.'); this.manemail.value=''; this.mainmail.focus(); return false}">

peach255
01-31-2003, 10:08 AM
think I'm close ... but no cigar yet. any idea? It just seems to bypass this script.


checkEmail = form.manemail.value

atsymbol = checkEmail.indexOf('@');
domainstart = atsymbol + 1;
wholedomain = checkEmail.substring(domainstart,checkEmail.length);
if (wholedomain <> "xyz.com");
{
alert('Need xyz.com');
form.manemail.select();
return false;
}

Charles
01-31-2003, 10:13 AM
How about trying my suggestion? And check out that link I gave above.

I think your problem has to do with your form itself. It's just a hunch, but a link to the page might be helpful.

khalidali63
01-31-2003, 10:34 AM
If you are hard coding a condition for a certain email address then that should be piece of cake.and your condition

if ((checkEmail.substring('xyz.com') >-1)

will have to work.

If you are getting a random string and only checking fora a valid email account then you may want to check this link below,
http://68.145.35.86/skills/javascripts/EmailValidator.html

Otherwise you must have to validate your text entry from the form( as charles mentioned above).