Solved

Best way to validate Phone Number and Email???

Posted on 2003-10-22
7
14,684 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
Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 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