Link to home
Start Free TrialLog in
Avatar of jeffmace
jeffmace

asked on

Best way to validate Phone Number and Email???

Can someone please post some code examples(javascript) that show how to validate a phone number.  I only want digits(example: 1231231234 NOT 123-123-1234).  If you know of a way to allow international numbers as well that would be great (example this is a phone number from Europe: Phone: +44 20 8390 xxxx).   I am not sure if there is a way to show that it is a USA number or a foreign number, but if it is possible that would be great.

Also what is the best way to check that a person typed in a valid email address(Example. test@aol.com NOT test@aol or whatever).

Thanks
SOLUTION
Avatar of CFDevHead
CFDevHead

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CFDevHead
CFDevHead

here is the email validation script
function isEmail (v) {
      var email = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
       if(!email.test(v)) {
            alert('Wrong')
      }
 }
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
take a look at this from another thread

<html>
<head>
<script language="javascript">
function validate(emailad) {
a = emailad.split(";")
var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
var check=/@[\w\-]+\./;
var checkend=/\.[a-zA-Z]{2,3}$/;
for(i=0;i<a.length;i++)
{
   emailadd = a[i];
   if(((emailad.search(exclude) != -1)||(emailad.search(check)) == -1)||(emailad.search(checkend)
== -1)){
        alert("Incorrect email address!");
        return false
   }
   else {
   alert('hello')
        //document.form.submit();
   }
}
}
</script>
</head>
<body>
<form method="post" action="this.htm">
<input type="text" name="email" onBlur="javascript:validate(this.value)";>
<input type="submit">
</form>

</body>
this can be the best possible

http://cflib.org/udf.cfm?ID=216

Example
<cfoutput>
#YesNoFormat(IsEmail("jguillaume@stoneage.com"))#<br>
#YesNoFormat(IsEmail("jguillaume@stoneage.com.info"))#<br>
#YesNoFormat(IsEmail("jguillaume@subdomain.stoneage.museum"))#<br>
#YesNoFormat(IsEmail("jguillaume@stoneage.invalidTLD"))#
</cfoutput>

Output
Yes
Yes
Yes
No

Hope that helps

K'Rgds
Anand
I use this Netscape RegEx written library:

http://developer.netscape.com/docs/examples/javascript/regexp/overview.html

It is really good, it has simple methods for everything.  US Number, SSN, International Number, Email Address, Dates, Zip Codes, Credit Cards, and it even has text box validation and has easy ways to warn the user with a message.

CJ
ne-luck ???