...

View Full Version : Phone Number Validation



bubble00000
06-22-2010, 10:54 AM
Need Phone Number Validation for my JavaScript, i can't work it out It currently has E-mail, Surname, Address and Name validation, This is my code at the moment:


<script language="JavaScript">
function echeck(str) {

var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){
alert("Please Enter a Valid E-mail")
return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
alert("Please Enter a Valid E-mail")
return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
alert("Please Enter a Valid E-mail")
return false
}

if (str.indexOf(at,(lat+1))!=-1){
alert("Please Enter a Valid E-mail")
return false
}

if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
alert("Please Enter a Valid E-mail")
return false
}

if (str.indexOf(dot,(lat+2))==-1){
alert("Please Enter a Valid E-mail")
return false
}

if (str.indexOf(" ")!=-1){
alert("Please Enter a Valid E-mail")
return false
}


return true
}

function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
{
alert(alerttxt);return false;
}
else
{
return true;
}
}
}
function validate_form(thisform)
{
with (thisform)
{
if (validate_required(Name,"Name must be filled out!")==false)
{Name.focus();return false;}
if (validate_required(Surname,"Surname must be filled out!")==false)
{Surname.focus();return false;}
if (validate_required(Country,"Address must be filled out!")==false)
{Country.focus();return false;}
}

{
var emailID=document.submitting.email

if ((emailID.value==null)||(emailID.value=="")){
alert("Please Enter a Valid E-mail")
emailID.focus()
return false
}
if (echeck(emailID.value)==false){
emailID.value=""
emailID.focus()
return false
}
return true
}
}

function verifyEmail(form) {
checkEmail = form.email.value

if ((checkEmail.indexOf('@') < 0) || ((checkEmail.charAt(checkEmail.length-4) != '.') && (checkEmail.charAt(checkEmail.length-3) != '.')))
{alert("You have entered an invalid email address. Please try again.");
form.email.select();
return false;
}

else {
form.method="get";
form.target="_self";
form.action="myscript.cgi";
form.submit();
}
}
</script>


<form action="submit.htm" onsubmit="return validate_form(this)" method="post" name="submitting">
<span class="style5">Select Plant:
<form id="form1" name="form1" method="post" action="">
<label>
<select name="Item" id="Item">
<option>OMGwoopwoop Plant</option>
<option>I'm Blue Plant </option>
<option>Anonymous Plant</option>
<option>Rawr Plant</option>
<option>Chicka Plant </option>
<option>Fruitopola Plant</option>
<option>Whitoe Plant</option>
<option>Wukidoo Plant </option>
</select>
</label>
</p>
<p>Name:<span class="style1">
<input type="text" name="Name" size="30">
<br>
<br>
</span>Surname:
<span class="style1">
<input type="text" name="Surname" size="30">
<br>
<br>
</span>Address:<span class="style1">
<input type="text" name="Country" size="30">
<br>
<br>
</span>Email:<span class="style1">
<input type="text" name="email" size="30">
<br></span></p>
Phone Number:<span class="style1">
<input type="text" name="phone" size="30">
<form action="submit.htm" onSubmit="return validate_form(this)" method="post">
<input type="submit" value="Submit">
</form>

Philip M
06-22-2010, 12:19 PM
This is ancient code. <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.

Your email validation is absurdly complicated. Use a regular expression:-


if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@((\w)([\w\-]?)+\.)+([a-z]{2,4})$/i.test(email.value))) { // Invalid email if returns true

You do not say what the required phone number format is. This will cover most formats including international.


if (!(^(\+44)?[0-9\s\-]{10,})$/gi.test(phoneNumber.value)) { /// telephone number, only digits, space, hyphen and plus sign as first character are allowed. Minimum of 10 characters. Invalid if returns true.

Validation of the form if (value==null||value=="") is barely worthy of the name, and virtually useless, as even a single space, an x or a ? will return false, that is pass the validation. This topic has been covered many times before in this forum.

filename.focus() does not work properly in Firefox.

<form action="submit.htm" onsubmit="return validate_form(this)" method="post" name="submitting">
<span class="style5">Select Plant:
<form id="form1" name="form1" method="post" action="">
You may not nest one form within another.

Here is modern validation for first name, last name, phone number, email:-


<html>
<head>
<body>

<form name="myForm" onSubmit="return checkForm();" action="processForm.php" method="post">

<input type="text" size="40" name="firstName" value="*First Name" onfocus="this.value = ''" /> <br />
<input type="text" size="40" name="lastName" value="Last Name" onfocus="this.value = ''" /> <br />
<input type="text" size="40" name="phoneNumber" value="Phone" onfocus="this.value = ''" /> <br />
<input type="text" size="40" name="emailAddress" value="*Email" onfocus="this.value = ''" /> <br />
<textarea rows="6" cols="30" onfocus="if (this.value =='Message') {this.value= ''}">Message</textarea>
<input type="submit" value="Submit" />

</form>
<script type = "text/javascript">

function checkForm(){
var fn = document.myForm.firstName;
fnval = fn.value;
fnval = fnval.replace(/^\s+|\s+$/g,""); // strip leading and trailing spaces
if((fnval.length <2) || (/[^a-z\-\']/gi.test(fnval))) { // hyphen and apostrophe may occur in name
alert ("Please enter your first name using only letters a-z");
document.myForm.firstName.value = "";
myfield = fn; // note myfield must be a global variable
setTimeout('myfield.focus();myfield.select();' , 10);
return false;
}

var ln = document.myForm.lastName;
var lnval = ln.value;
if (lnval != "Last Name") {
lnval = lnval.replace(/^\s+|\s+$/g,""); // strip leading and trailing spaces
if((lnval.length <2) || (/[^a-z\-']/gi.test(lnval))) { // hyphen and apostophe may occur in name
alert ("Please enter your last name using only letters a-z, and hyphen/apostrophe if appropriate");
document.myForm.lastName.value = "";
myfield = ln; // note myfield must be a global variable
setTimeout('myfield.focus();myfield.select();' , 10);
return false;
}
}

var pn = document.myForm.phoneNumber;
var pnval= pn.value;
if (pnval !="Phone") {
pnval = pnval.replace(/\s/g,""); // strip all spaces
if((pnval.length <10) || (/[^0-9\-]/gi.test(pnval))) { // only digits and hyphen allowed
alert ("Please enter your phone number - only digits and hyphen allowed");
document.myForm.phoneNumber.value = "";
myfield = pn; // note myfield must be a global variable
setTimeout('myfield.focus();myfield.select();' , 10);
return false;
}
}

var em = document.myForm.emailAddress;
var emval = em.value;
if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/i.test(emval))) {
alert ("Invalid email address - please re-enter it");
document.myForm.emailAddress.value = "";
myfield = em; // note myfield must be a global variable
setTimeout('myfield.focus();myfield.select();' , 10);
return false;
}

}

</script>
</body>
</html>

"In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)

Sciliano
06-22-2010, 01:02 PM
bubble:

You may want to consider this:

http://www.freewarefiles.com/Plug-n-Play-Contact-Us-Form_program_53638.html

bubble00000
06-22-2010, 01:03 PM
With your code you said that your able to do first name, last name, phone number, email. It only validates First Name and E-mail, the JavaScript only validates Phone number or last name when you enter something in but if you were to press the submit button it would still work and not ask for you to enter anymore. What would be the solution to that?

& thank you for you generous help so far! :)

Sciliano
06-22-2010, 01:18 PM
bubble:

Did you see my previous post?

bubble00000
06-22-2010, 01:34 PM
Yeah i saw your previous post, the problem i have with it is that its more experienced then what i am needing. but thank you for your contribution :)

bubble00000
06-22-2010, 01:40 PM
I got it working now :)
I'd give a special thanks to Philip M cause i got the majority of my answers off http://codingforums.com/showthread.php?t=198661 :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum