|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Error trapping x@x.xxx in form
Hi all,
I have a javascript function I use for ensuring that the person filling in my form fills out each of the fields. This works well, but there is another feature I would like to add to it, but do not know what the syntax is, nor how I would be able to incorporate it into my current script without breaking it. I would like to ensure that not only does the person fill out all fields, including the email address field, but that if he/she was to fill out the email field with n/a or something that is not x@x.xxx (in other words a valid email string), it returns an alert alike to the ones I already use (in this case, telling the user to enter a real email address) Any help is appreciated This is my current script: <!-- function checkform ( form ) { // ** START ** if (form.name.value == "") { alert( "Please enter your full name." ); form.name.focus(); return false ; } // ** END ** // ** START ** if (form.address.value == "") { alert( "Please enter your full address." ); form.address.focus(); return false ; } // ** END ** // ** START ** if (form.postcode.value == "") { alert( "Please enter your postcode." ); form.postcode.focus(); return false ; } // ** END ** // ** START ** if (form.phone.value == "") { alert( "Please enter your phone number, including area code." ); form.phone.focus(); return false ; } // ** END ** // ** START ** if (form.emailaddr.value == "") { alert( "Please enter your correct email address." ); form.emailaddr.focus(); return false ; } // ** END ** return true ; } //--> |
|
#2
|
|||
|
|||
|
RE: Error trapping x@x.xxx in form
Okay - I would go for a little bit of pattern matching (try the following for a taste):
Code:
<html>
<head>
<script type="text/javascript">
<!--
var pattern = new RegExp("(\w+)(@)(\w+)(\.)((\w{3})||\w{2}\.\w{2})$");
var text = "someone@somedomain.co.uk";
// could also be someone@somedomain.com
var result = pattern.test(text);
document.write(result);
//-->
</script>
</head>
<body>
</body>
</html>
if you look at the line: RegExp("(\w+)(@)(\w+)(\.)((\w{3})||\w{2}\.\w{2})$"); what this does is matches a string and returns true or false if a good match is found - here we go: (\w+) match any number of characters (@) match an @ symbol (\w+) then any characters again (\.) then a fullstop then (and I added a bit more) ((\w{3})||\w{2}\.\w{2}) match any 3 characters (e.g. com) OR any 2, a fullstop, then another 2 (e.g. co.uk) The $ on the end means the pattern must match from the end of the string (i.e.) .com OR .co.uk at the end. Hope this helps. |
|
#3
|
|||
|
|||
|
RE: Error trapping x@x.xxx in form
Thank you for your very full response, that makes perfect sense... I'll give it a bash.
|
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Client Side Things > Error trapping x@x.xxx in form |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|