I need to check a string to make sure it includes a period "." and does not include an "@" or spaces but it doesn't matter what else is in it.
Any help is appreciated.
Thanks in advance.
| SitePoint Sponsor |
I need to check a string to make sure it includes a period "." and does not include an "@" or spaces but it doesn't matter what else is in it.
Any help is appreciated.
Thanks in advance.
thansk for the quick reply - javascript





Ack.
Sorry, but i'm afraid I can't help much. With VBScript and stuff I can do it in a snap, but my JavaScript skills are VERY VERY rusty.
I'll turn this thread over to one of our resident JS gurus now.
Sorry that I couldn't help.
Good luck with your Scripting!![]()


Hey guys,
To detect if a string contains a character, you can use indexOf. Here's an example:
Hope that helps,Code:str="this is a new string with @ and, of course, spaces"; if(str.indexOf(".")==-1 || str.indexOf(" ")!=-1 || str.indexOf("@")!=-1){ alert('This string is not in the correct format'); }
aDog![]()
Moderator at www.javascriptcity.com/forums/
You can also use regular expression in Javascript to check whether a string contains " . " and anything else other than "@"
<script language="javascript1.2">
re = /^[\w\d\.]+.[\w\d\.]+$/
myString = "abc@ew e";
if (re.test(myString)) {
alert("valid string!");
}
else{ alert("invalid string!"); }
myString2 = "accept.this";
if (re.test(myString2)) {
alert("valid string!");
}
else{ alert("invalid string!"); }
</script>
This regular expression accepts any letter or digit and at least a period. Probably not very complete, but you can go here for more reference: http://developer.netscape.com/docs/m...ide/regexp.htm
Bookmarks