Click to See Complete Forum and Search --> : Enter valid Phone (form validation)


KevEOS
12-17-2003, 10:16 AM
Hi all
I am new for the javascript form validation and I really appreciate for your help here.

I create a form to allow people enter their information such as email address, phone number etc. And I would like to make a validation form to enter for a valid value e.g: in phone:_______ if they enter letter from a-z or not numberic the popup window alert "please enter your valid phone number".
I tried to so but it doesn't work. What did I do wrong in the javascript below:

<script language="javascript">
<!--
function ValidateForm()
{

var phone = parseInt(document.form1.phone.value);
var validphone = parseInt(/[A-Z][a-z]/);

if (document.form1.phone.value == "" && document.form1.phone.value == validphone) {
window.alert("Please enter your valid phone number");
return false;
}

return true;
}
// -->
</script>


Form *.html format:
<form name="form1" method="POST" onSubmit="return ValidateForm()">

<td bgcolor="#CCCCCC"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">Phone: </font></td>

<td bgcolor="#CCCCCC"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">
<input type="text" name="Phone" size="15">Fax:
<input type="text" name="Fax" size="15">
E-Mail:
<input type="text" name="EMail" size="15">
</font></td>


Thanks so much,
Kev.

requestcode
12-17-2003, 12:07 PM
Here is an example using regular expressions that checks to see if the phone number is in the format of(999)999-9999

Perhaps you can modify it for your needs.
<html>
<head>
<title>check for numbers and letters</title>
<script language="JavaScript">
// use regular expressions to check for numbers
rephn=/[(]\d{3}[)]\d{3}[-]\d{4}/ // used to check for phone number format of (999)999-9999
// 9's representing numbers only

function chkphone(formid) // check for valid phone number format
{
if(formid.phnum.value.match(rephn))
{
alert("Ok!")
}
else
{
alert("Invalid Format!\nMust be left paranthesis \'(\', three numbers, right parenthesis \')\',three numbers a dash and then four numbers.ex:(123)456-7890")
formid.phnum.focus()
formid.phnum.select()
}
}
</script>
</head>
<body bgcolor="lightgreen">
<center><h1>Pattern Matching and Regular Expressions</h1></center>
<form name="myform">
<b>Phone Number:</b><input type="text" name="phnum" value="(999)999-9999" onfocus="this.select()" size="13" maxlength="13">
<input type="button" value="Check for correct phone number format." onClick="chkphone(this.form)">
</form>
<b><font color="blue">Here are some links to some excellent reference material on Pattern Matching and Regular Expressions:</font></b><br>
<a href="http://www.webreference.com/js/column5/index.html" target="_new">Pattern Matching and Regular Expressions Tutorial</a><br>
<a href="http://developer.netscape.com:80/docs/manuals/js/core/jsref/regexp.htm" target="_new">Regular Expression Documentation</a><br>
<center><b><a href="#" onClick="self.close()">Close Window</a></b></center>
</body>
</html>