Solved

Best way to validate Phone Number and Email???

Posted on 2003-10-22
7
14,677 Views
Last Modified: 2013-12-24
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
Comment
Question by:jeffmace
  • 4
  • 2
7 Comments
 
LVL 9

Assisted Solution

by:CFDevHead
CFDevHead earned 250 total points
ID: 9599878
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
 
LVL 9

Expert Comment

by:CFDevHead
ID: 9600806
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
 
LVL 17

Accepted Solution

by:
anandkp earned 250 total points
ID: 9600836
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 17

Expert Comment

by:anandkp
ID: 9600889
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
 
LVL 17

Expert Comment

by:anandkp
ID: 9600900
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
 
LVL 19

Expert Comment

by:cheekycj
ID: 9606001
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
 
LVL 17

Expert Comment

by:anandkp
ID: 9612847
ne-luck ???
0

Featured Post

Scale it in WD Gold

With up to ten times the workload capacity of desktop drives, WD Gold hard drives employ advanced technology to deliver among the best in reliability, capacity, power efficiency and performance.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now