SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
Thread: string validation - easy one
-
Mar 7, 2001, 16:35 #1
- Join Date
- Mar 2001
- Posts
- 2
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Mar 7, 2001, 16:43 #2
-
Mar 7, 2001, 16:45 #3
- Join Date
- Mar 2001
- Posts
- 2
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thansk for the quick reply - javascript
-
Mar 7, 2001, 19:01 #4
- Join Date
- Sep 2000
- Location
- United States
- Posts
- 1,921
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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!
-
Mar 7, 2001, 23:56 #5
- Join Date
- Jul 1999
- Location
- SC, USA
- Posts
- 390
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hey guys,
To detect if a string contains a character, you can use indexOf. Here's an example:
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'); }
aDogModerator at www.javascriptcity.com/forums/
-
Mar 15, 2001, 17:41 #6
- Join Date
- Feb 2001
- Posts
- 14
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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