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?
 
MorcalavinConnect With a Mentor Commented:
...
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
 
itortuAuthor Commented:
do you think you can show me how to use IsNum, instead that using a regular expression?

thank you though.
0
 
HonorGodCommented:
What is "IsNum"?  It isn't a standard / builtin method.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
TwitchletConnect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.