• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 14708
  • Last Modified:

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
0
jeffmace
Asked:
jeffmace
  • 4
  • 2
2 Solutions
 
CFDevHeadCommented:
try this
<script>
function formIsNumeric(v) {

      v = v.replace(/\s+$|^\s*/gi, "");
      isNum = new RegExp ("[^0-9]");
      if (isNum.test(v)) {
            alert('Wrong')
      }
}

</script>

<form name="f">
<input type="text" name="t">
<input type="button" value="click here" onClick="formIsNumeric(f.t.value)">
</form>
0
 
CFDevHeadCommented:
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')
      }
 }
0
 
anandkpCommented:
Neat Script for phone no :)

use this for emails
<script>
function varemail()
{
var sCont = document.frm.email;
var sContVal = document.frm.email.value;

var str=new String();
str=sContVal;
var span=new RegExp("[ ]","g");
var rep=str.replace(span,"9");            

if (sContVal.length != 0)
      {
            if(sContVal == ''  || isNaN(rep) == false)
                  {
                        alert("Please enter email properly [ anand@direct2s.com ]");
                        sCont.focus();
                        sCont.select();
                        return false;
                  }                              
                              
            var sAtSym    = sContVal.indexOf('@')
            var sAtSym2        = sContVal.lastIndexOf('@')
            var sPeriod   = sContVal.lastIndexOf('.')
            var sSpace    = sContVal.indexOf(' ')
            var sLength   = sContVal.length - 1  
            
            if ( (sAtSym < 2) || (sAtSym != sAtSym2) || (sPeriod <= sAtSym+2) || (sPeriod == sLength ) || (sSpace  != -1) )                  
               {
                        alert("Please enter email properly [ anand@direct2s.com ]");
                        sCont.focus();
                        sCont.select();
                        return false;
               }
             }
}

there's also server chk's for emails - will send u that as well

K'Rgds
Anand
0
Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

 
anandkpCommented:
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>
0
 
anandkpCommented:
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
0
 
cheekycjCommented:
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
0
 
anandkpCommented:
ne-luck ???
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now