Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

validate zip code using isnum

Posted on 2006-11-02
11
Medium Priority
?
951 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 400 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Assisted Solution

by:Twitchlet
Twitchlet earned 400 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
 

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

824 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