Solved

Best way to validate Phone Number and Email???

Posted on 2003-10-22
7
14,674 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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
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

Network it in WD Red

There's an industry-leading WD Red drive for every compatible NAS system to help fulfill your data storage needs. With drives up to 8TB, WD Red offers a wide array of solutions for customers looking to build the biggest, best-performing NAS storage solution.  

Join & Write a Comment

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 …
In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

13 Experts available now in Live!

Get 1:1 Help Now