Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

function isnumeric : to submit or not submit form?

mwpclark (Programmer)
9 Sep 11 19:06
Hello

I am using a form to search by zip code.  If the visitor does not enter a 5-digit zip code, an alert is generated.  So far, so good.  Search works, alert works.

However if it returns false, when the OK is clicked on the alert, the form submits anyway.  I am seeking a method that will return to the referring page if the zip code entry is not valid, rather than submitting the form.  

in <head> :

CODE


<script language="javascript" type="text/javascript">
    function isNumeric(elem, helperMsg){
    var numericExpression = /^\d{5}$/;
    if(elem.value.match(numericExpression)){
        return true;
    }
    else{
        alert(helperMsg);
        elem.focus();
        return false;
    }
}
</script>

in <body>

CODE


<input type="submit" value="GO!" style="font-weight:bold;" onclick="isNumeric(document.getElementById('numbers'), '5-digit Zip Code Please!')">

I have tried <input type="button", which does not submit the form at all.  I have tried putting the onclick routine into the <form> tag as an "onSubmit", same results, submits the form when false.

I have tried reversing the true and false elements in the first <script>

CODE


    if(!(elem.value.match(numericExpression))){
        alert(helperMsg);
        elem.focus();
        return false;
    }
    else{
        return true;
    }
Submits form, generates alert, but submits form when click OK on alert.  Still no joy.

Any suggestions much appreciated....

One example on www.physioyellow.com

Thanks in advance...

Cheers
Mike
vacunita (Programmer)
9 Sep 11 20:48
Basically you need to return the value from the onclick event or from the onsubmit event.

In other words add a return to your function call:

CODE

<input type="submit" value="GO!" style="font-weight:bold;" onclick="return isNumeric(document.getElementById('numbers'), '5-digit Zip Code Please!')">

If your function returns a false value the onClick will return that value as well and cancel the action.  

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
http://behindtheweb.blogspot.com/
 

mwpclark (Programmer)
9 Sep 11 21:03
Works like a champ.  Muchisimas Gracias

 

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close