Lisa9999
09-27-2003, 11:07 PM
Hello - I am trying to validate a form using a script from Tom Negrino's Javascript book. Thought I had it working perfectly however if I enter a numeric value and follow it with alpha characters.. the script does not return an error like it does if you begin with an alpha character. ?
This is the code I'm using to verify a phone number:
function isNum(passedVal) {
if (passedVal == "") {
alert("You must enter your phone number") //cannot have an empty Phone Field
return false;
}
for (i=0; i < passedVal.length; i++) {
if ((passedVal.charAt(i) < "0") || (passedVal.charAt(i) > "9")) {
alert("Your phone number must be numeric and 10 digits") //must be numeric
return false
}
if (passedVal.length < "10") {
alert("Your phone number must be 10 digits") //must be 10 digits
return false
}
return true
}
}
------------- ( Here are the specs on the form fields.. i omitted to enter them when I first posted)
<body bgcolor="#CCFF99">
<form onsubmit="return submitIt(this)" action="yballoonfinalize.html" method="get" name="myform" target="_self" id="myform" >
<p><img src="balloon header.jpg" alt="balloon LOGO" width="300" height="75" />
</p>
<p class="bold">Please - No Slashes/Dashes/or Hyphens! </p>
<p></p>
<span class="bold">Name:
<input name="userName" type="text" id="userName" onblur="setCookie()" onchange="validuserName(this.value)" size="15" maxlength="20" />
</span></p>
<p class="bold"> Phone Number:
<input name="Phone" type="text" class="bold" id="Phone" onchange="isNum(this.value)" size="10" maxlength="10" length="10" />
</p>
<p class="bold">Credit Card Number:
<input name="Ccard" type="text" class="bold" id="Ccard" onchange="isNum2(this.value)" size="16" maxlength="16" length="16" />
</p>
<p class="bold">Expiration Date:
<input name="Expdate" type="text" class="bold" id="Expdate" onchange="isNum3(this.value)" size="4" maxlength="4" length="4" />
</p>
<p class="bold">Balloon Color:
<input name="yellowField" type="text" readonly="true" id="yellowField" value="yellow" size="10" maxlength="10" onblur="setCookie()" />
</p><p>
<input name="Submit" type="submit" value="Submit" />
</p></span></form></body>
Where have I gone wrong?
Thanks!
This is the code I'm using to verify a phone number:
function isNum(passedVal) {
if (passedVal == "") {
alert("You must enter your phone number") //cannot have an empty Phone Field
return false;
}
for (i=0; i < passedVal.length; i++) {
if ((passedVal.charAt(i) < "0") || (passedVal.charAt(i) > "9")) {
alert("Your phone number must be numeric and 10 digits") //must be numeric
return false
}
if (passedVal.length < "10") {
alert("Your phone number must be 10 digits") //must be 10 digits
return false
}
return true
}
}
------------- ( Here are the specs on the form fields.. i omitted to enter them when I first posted)
<body bgcolor="#CCFF99">
<form onsubmit="return submitIt(this)" action="yballoonfinalize.html" method="get" name="myform" target="_self" id="myform" >
<p><img src="balloon header.jpg" alt="balloon LOGO" width="300" height="75" />
</p>
<p class="bold">Please - No Slashes/Dashes/or Hyphens! </p>
<p></p>
<span class="bold">Name:
<input name="userName" type="text" id="userName" onblur="setCookie()" onchange="validuserName(this.value)" size="15" maxlength="20" />
</span></p>
<p class="bold"> Phone Number:
<input name="Phone" type="text" class="bold" id="Phone" onchange="isNum(this.value)" size="10" maxlength="10" length="10" />
</p>
<p class="bold">Credit Card Number:
<input name="Ccard" type="text" class="bold" id="Ccard" onchange="isNum2(this.value)" size="16" maxlength="16" length="16" />
</p>
<p class="bold">Expiration Date:
<input name="Expdate" type="text" class="bold" id="Expdate" onchange="isNum3(this.value)" size="4" maxlength="4" length="4" />
</p>
<p class="bold">Balloon Color:
<input name="yellowField" type="text" readonly="true" id="yellowField" value="yellow" size="10" maxlength="10" onblur="setCookie()" />
</p><p>
<input name="Submit" type="submit" value="Submit" />
</p></span></form></body>
Where have I gone wrong?
Thanks!