Has the user entered text in a numeric field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • unkietee
    New Member
    • Mar 2008
    • 4

    Has the user entered text in a numeric field

    Hi Experts, my first post here! I hope you can help me.

    I have a basic form which I want the user to enter either a number (postcode/zipcode) or text (suburb).

    When they hit submit I want to check what has been entered and then send them to either a postcode/zip code page if it was a number entered or send them to a suburb page if it was text they entered.

    Is this possible and if so how?

    Thanks for your possible help.

    Unkietee
  • karthickkuchanur
    New Member
    • Dec 2007
    • 156

    #2
    Originally posted by unkietee
    Hi Experts, my first post here! I hope you can help me.

    I have a basic form which I want the user to enter either a number (postcode/zipcode) or text (suburb).

    When they hit submit I want to check what has been entered and then send them to either a postcode/zip code page if it was a number entered or send them to a suburb page if it was text they entered.

    Is this possible and if so how?

    Thanks for your possible help.

    Unkietee
    call the function in while submit the button
    check the condition in js whether it a number or an test ans link the page that u wnt to do
    Note:NAN check

    Comment

    • unkietee
      New Member
      • Mar 2008
      • 4

      #3
      Thanks karthickkuchanu r,

      I understand the concept - I dont understand how to write it!

      Do you have a sample I could look at?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        You could call the function onsubmit.

        To change the page that the form is submitted to, use the action property to point to the correct URL.

        Do you know how to test if it's an integer?

        Comment

        • unkietee
          New Member
          • Mar 2008
          • 4

          #5
          Thanks Acoder,

          I am not 100% on how to check if its an integer. The code below is as close as I have got but it is not working - what am I doing wrong?


          Code:
          <script language="Javascript">
                function chk_val(obj) {
             
                    var chk = /^\d+$/.test(obj.value);
             
                   
             
                    if (!chk) {
             
                        location.href = 'SuburbSearch.php';
             
                        obj.focus();
             
                    }
             
                }
          </script>
          <body>
          
          <h1>Start Here</h1>
          
          <form action="PostcodeSearch.php" method="post">
          
          
          Enter Postcode:<br>
          
          <input name="searchterm" type=text value="" onblur="chk_val(this);">
          
          <br>
          </form>

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Instead of changing the location.href, change the form's action property and then call its submit() method.

            Comment

            • FredSovenix
              New Member
              • Mar 2008
              • 10

              #7
              Side Tip: You mentioned "post code" in addition to ZIP code in your original post, so if your application is targeting international, don't forget that British-derived postal codes contain text characters. (If not for international use, please ignore this post from the peanut gallery.)

              Comment

              • unkietee
                New Member
                • Mar 2008
                • 4

                #8
                Thanks FredSovenix,

                I am only using postcodes of Australia but point taken.

                Now to try acoders suggestion and see if I can get it to work.

                Back soon!

                Comment

                • hedgomatic
                  New Member
                  • Oct 2009
                  • 1

                  #9
                  Code:
                  function chk_val(i) {
                    var chk= parseInt(i);
                    if(chk==NaN) { return false; } else { return true; }
                  };
                  
                  
                  var n=  chk_val(userInput);
                  
                  if(n) { 
                  // allow user to do xyz
                  } else {
                  // not a number! do something else.
                  }
                  Last edited by gits; Oct 28 '09, 04:18 AM. Reason: added code tags

                  Comment

                  Working...