Solved

validate zip code using isnum

Posted on 2006-11-02
11
925 Views
Last Modified: 2008-02-01
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>
0
Comment
Question by:itortu
11 Comments
 
LVL 18

Accepted Solution

by:
Morcalavin earned 100 total points
ID: 17858749
...
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
 

Author Comment

by:itortu
ID: 17858767
do you think you can show me how to use IsNum, instead that using a regular expression?

thank you though.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 17858832
What is "IsNum"?  It isn't a standard / builtin method.
0
 
LVL 2

Assisted Solution

by:Twitchlet
Twitchlet earned 100 total points
ID: 17858839
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
 
LVL 2

Expert Comment

by:Twitchlet
ID: 17858841
Wow, three replies while I was typing that...
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:itortu
ID: 17858997
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
 

Author Comment

by:itortu
ID: 17859086
ithe javascript console tells me that isnum is not defined.
0
 
LVL 2

Expert Comment

by:Twitchlet
ID: 17859136
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
 

Author Comment

by:itortu
ID: 17859168
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
 

Author Comment

by:itortu
ID: 17859175
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
 
LVL 2

Expert Comment

by:Twitchlet
ID: 17859215
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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now