posted 19 years ago
I am still pretty new to JS and am having a problem determining how to validate alpha and numeric enteries on a form. The examples below are for a zip code and name field. If a user enters an alpha char in the zip code field, then I want an alert to display that requests the user to please enter only digits for the zip code (additionally, I would prefer the user have the ability to enter 5 and 10 chars...first, the 5-digit zip then the dash + four. If they only have a 5-digit zip, then validate only the 5-digit zip). The same rule will apply for the name field: only validate alpha an not numeric chars.
Thanks
<script type='text/javascript'>
function mySubmission()
{
if (window.document.myContactForm.myZip.value.length!=5)
{
window.alert("Your zip code can only be 5 digits long.");
return false;
}
if (window.document.myContactForm.myFirstName.value=="")
{
window.alert("You must enter your First Name
return false;
submission
}
}
</script>
<body>
<form
title="Contact Form"
name='myContactForm'
id='myContactForm'
enctype="text/plain"
action="mailto:info@mustardseedcompanyconsultants.com"
method="post"
onSubmit='return mySubmission();'
onReset="">
<!-- Table-->
<table border='1'
width='64%'>
<tr>
<td style="font-family:verdana,tahoma,arial;font:10pt;"><strong>First name:</strong></td>
<td>
<!-- Text field for entering name -->
<input
type="text"
name="myFirstName"
id='myFirstName'
size="20"
maxlength="20"
alt="firstName"
value="" /></td>
</tr>
<tr>
<td>
<table border='1'
width='19%'>
<tr>
<td style="font-family:verdana,tahoma,arial;font:9pt;"><strong>Zip/Postal: </strong></td>
<td>
<!-- Text field for zip/postal code -->
<input
type="text"
name='myZip'
id='myZip'
size="20"
maxlength="20"
alt="zip"
value="" /></td>
</tr>
</table>
</td>
</tr>
</td>
</table>
</form>
</body>