Solved

Best way to validate Phone Number and Email???

Posted on 2003-10-22
7
14,680 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

772 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