validate zip code using isnum

hello i would like to know how to use the Isnum function to validate a numeric field.
I have a simple form with a simple javascript validation function, that checks for empty
values. i need to incorporate to the function a validation for the zip code using IsNum,
but I don't understand how to place this function for a single field, inside the form validation function.

any help would be great, thank you.

my function:

<script language="javascript">
<!--

function checkForm(formObj){

if(document.myform.fname.value == ""){

  alert("Please enter your name in the first field.");
  return false;
}
if(document.myform.lname.value == ""){
  alert("Please enter your last name in the second field.");
  return false;
}
if(document.myform.ad1.value == ""){
  alert("Please enter your address in the third field.");
  return false;
}
if(document.myform.city.value == ""){
  alert("Please enter your city in the fourth field.");
  return false;
}
if(document.myform.state.value == ""){
  alert("Please enter your state in the fifth field.");
  return false;
}
if(document.myform.zip.value == ""){
  alert("Please enter your zip code in the sixth field.");
  return false;
}

return true;
}

//-->
</script>

my form:

<form onSubmit="return checkForm(this);" name="myform">
<div style="text-align: center">
    <table border=1 width="400px">
        <tr>
            <td width="100px">First Name:</td>
            <td  width="300px"colspan="3"><input type="text" value="" size=35 maxlength=35 id="fname"/>
        </tr>
        <tr>
            <td>Last Name:</td>
            <td colspan="3"><input type="text" value="" size=35 maxlength=35 id="lname"/></td>
        </tr>
        <tr>
            <td>Address1:</td>
            <td colspan="3"><input type="text" value="" size=35 maxlength=35 id="ad1"/></td>
        </tr>
        <tr>
            <td>Address2:</td>
            <td colspan="3"><input type="text" value="" size=35 maxlength=35 /></td>
        </tr>
        <tr>
            <td>City:</td>
            <td colspan="3"><input type="text" value="" id="city"/></td>
        </tr>
        <tr>
            <td>State:</td>
            <td><input type="text" value="" size="2" maxlength="2" id="state"/></td>
           <td>Zip:</td>
            <td colspan="3"><input type="text" value="" size="5" maxlength="5" id="zip"/></td>
        </tr>
        <tr>
            <td colspan="2" align="right"><input type="submit" value="Submit" /></td>
          <td colspan="2" align="left"><input type="reset" value="Reset" /></td>
        </tr>
    </table>
</div>
</form>
itortuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MorcalavinCommented:
...
if(document.myform.zip.value == ""){
  alert("Please enter your zip code in the sixth field.");
  return false;
}
if(!document.myform.zip.value.match(/^\d{1,}$/)){
  alert("Your zip code isn't numeric.");
  return false;
}
...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
itortuAuthor Commented:
do you think you can show me how to use IsNum, instead that using a regular expression?

thank you though.
0
HonorGodSoftware EngineerCommented:
What is "IsNum"?  It isn't a standard / builtin method.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

TwitchletCommented:
Hi,

I have no idea about the IsNum function, but have you considered using Regular Expressions to do the test. Something like the modification below;

-----

  alert("Please enter your state in the fifth field.");
  return false;
}
var rx = new RegExp('\\d{9}','gi');
var f = "123456789";
if(f == "" || (!rx.test(f) && f.length != 9)){
  alert("Please enter your zip code in the sixth field.");
  return false;
}

-----

would check for text comprised of only 9 numeric characters and throw the alert if that check fails...
0
TwitchletCommented:
Wow, three replies while I was typing that...
0
itortuAuthor Commented:
i was trying something like this:
<script language="javascript">
<!--

function validZip(inZip) {                              // Is this a valid Zip code?
  if (inZip == "") {
  return true
}
  if (isNum(inZip)) {                                    // Check if Zip is numeric
  return true
}
return false
}

function checkForm(formObj){

if(document.myform.fname.value == ""){

  alert("Please enter your name in the first field.");
  return false;
}
if(document.myform.lname.value == ""){
  alert("Please enter your last name in the second field.");
  return false;
}
if(document.myform.ad1.value == ""){
  alert("Please enter your address in the third field.");
  return false;
}
if(document.myform.city.value == ""){
  alert("Please enter your city in the fourth field.");
  return false;
}
if(document.myform.state.value == ""){
  alert("Please enter your state in the fifth field.");
  return false;
}
if(document.myform.zip.value == ""){
  alert("Please enter your zip code in the sixth field.");
  document.myform.zip.focus()
  return false
}
if (!validZip(document.myform.zip.value)) {
  alert("That is an invalid Zip code")
  document.myform.zip.focus()
  document.myform.zip.select()
  return false
}

return true;
}

//-->
</script>

but it seems not to respect the isnum function, i don't know if ai am calling it correctly.
0
itortuAuthor Commented:
ithe javascript console tells me that isnum is not defined.
0
TwitchletCommented:
I've worked out that there is no such standard language function as isNum(); I have found a couple of people's implementations of one on the web though in my searching, where did you get that?
0
itortuAuthor Commented:
i got the script example here:

http://www.javascriptworld.com/scripts/script08.11.html

I figured out what I was doing wrong. here is the implementation of the isnum sample on my simple validation function.

<script language="javascript">
<!--
function isNum(passedVal) {                              // Is this a number?
  if (passedVal == "") {
  return false
}
for (i=0; i<passedVal.length; i++) {
  if (passedVal.charAt(i) < "0") {
  return false
}
  if (passedVal.charAt(i) > "9") {
  return false
}
}
return true
}

function validZip(zip) {                              // Is this a valid Zip code?
  if (zip == "") {
  return true
}
  if (isNum(zip)) {                                    // Check if Zip is numeric
  return true
}
return false
}

function checkForm(formObj){

if(document.myform.fname.value == ""){
  alert("Please enter your name in the first field.");
  document.myform.fname.focus()
  return false;
}
if(document.myform.lname.value == ""){
  alert("Please enter your last name in the second field.");
  document.myform.lname.focus()
  return false;
}
if(document.myform.ad1.value == ""){
  alert("Please enter your address in the third field.");
  document.myform.ad1.focus()
  return false;
}
if(document.myform.city.value == ""){
  alert("Please enter your city in the fourth field.");
  document.myform.city.focus()
  return false;
}
if(document.myform.state.value == ""){
  alert("Please enter your state in the fifth field.");
  document.myform.state.focus()
  return false;
}
if(document.myform.zip.value == ""){
  alert("Please enter your zip code in the sixth field.");
  document.myform.zip.focus()
  return false
}
if (!validZip(document.myform.zip.value)) {
  alert("That is an invalid Zip code")
  document.myform.zip.focus()
  document.myform.zip.select()
  return false
}

return true;
}

//-->
</script>
0
itortuAuthor Commented:
i will add and split the point evenly between morcalavin and twichtlet, since they boht shared nice examples.

for your kind help, thank you : )
0
TwitchletCommented:
Schweet, but if you want to implement the validation using an isNum() function, there's a fairly nice implementation of a whole generic is[thing] function at http://www.felgall.com/jstip56.htm.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.