• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Validating alpha and numeric chars in a form

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the isNAN function
http://www.powermct.co.kr/teched/ecma/isnan.html

if(document.myContactForm.myZip.value.length!=5 && !isNAN(document.myContactForm.myZip.value))

You could also look at using regular expressions too....

Eric
 
David Lindsey
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code is not working. Here's how I have it set up:

function mySubmission()
{
if (window.document.myContactForm.myZip.value.length!=5 && !isNAN(window.document.myContactForm.myZip.value))

{
window.alert("Your zip code can only be 5 digits long.");
return false;
}
}

Any clues?

Thanks
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it should be isNaN NOT isNAN

my mistake!
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ugh, there is still a problem I think! I will fix it!
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DUH, my brain is fried:

That is what it should be
if(document.myContactForm.myZip.value.length!=5 || isNaN(document.myContactForm.myZip.value)

It should have been an or and not an and

Eric
 
David Lindsey
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, it is still not working because it recognizes alpha and numeric chars. I am wanting it to ONLY validate numeric chars.

Please review my initial correspondence =)

Thanks
 
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic