Sonia
07-28-2003, 05:04 AM
how do I validate an email textbox to check if the user has not entered 2 @ signs.
|
Click to See Complete Forum and Search --> : @ signs Sonia 07-28-2003, 05:04 AM how do I validate an email textbox to check if the user has not entered 2 @ signs. Gollum 07-28-2003, 05:23 AM you could use the String.match() method... if ( addr.match(/[@]/g).length != 1 ) alert("wrong number of '@' signs"); Charles 07-28-2003, 05:57 AM This one checks your field against all of the email address naming rules: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="Content-Script-Type" content="text/javascript"> <title>Example</title> <form action=""> <script type="text/javascript"> <!-- String.prototype.isEmailAddress = function () {return this.match(/^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+$/)} // --> </script> <div> <input type="text" onchange="if (!this.value.isEmailAddress()) {alert ('That would not appear to be a valid email address.'); this.value=''; this.focus()}"> <input type="submit"> </div> </form> Sonia 07-28-2003, 06:00 AM Can anyone debug that for me! <html> <head> <script> function val() { if (document.frm.addr.match(/[@]/g).length != 1 ) { window.alert("wrong number of '@' signs"); } } </script> </head> <body> <form name = "frm"> Email<input type = "text" name = addr> <input type = button value = test onClick= "val()"> </form> </body> </html> Gollum 07-28-2003, 06:07 AM The error is because you are calling document.frm.addr.match() and document.frm.addr.match is an input element, you need a string, so try... document.frm.addr.value.match(...) I also realised that if there are no '@' signs, then the function match() returns null, not an array with length zero, so you need to say var a = document.frm.addr.value.match(/[@]/g); if ( !a || (a.length != 1) ) alert('...'); Charles 07-28-2003, 06:09 AM Or just use the one that I posted above. Sonia 07-28-2003, 06:21 AM Cool! Working now but wat does the 'g' stands for in the code? <html> <head> <script> function val() { var a = document.frm.addr.value.match(/[@]/g); if ( !a || (a.length != 1) ) alert('only 1 @ sign'); } </script> </head> <body> <form name = "frm"> Email<input type = "text" name = addr> <input type = button value = test onClick= "val()"> </form> </body> </html> Charles 07-28-2003, 06:51 AM The "g" is a flag and it indicates that the pattern should be matched globally. See http://devedge.netscape.com/library/manuals/2000/javascript/1.3/guide/regexp.html. webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |