Connecting Tech Pros Worldwide Help | Site Map

Has the user entered text in a numeric field

Newbie
 
Join Date: Mar 2008
Posts: 4
#1: Mar 8 '08
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's Avatar
Familiar Sight
 
Join Date: Dec 2007
Posts: 135
#2: Mar 8 '08

re: Has the user entered text in a numeric field


Quote:

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
Newbie
 
Join Date: Mar 2008
Posts: 4
#3: Mar 8 '08

re: Has the user entered text in a numeric field


Thanks karthickkuchanur,

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

Do you have a sample I could look at?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Mar 8 '08

re: Has the user entered text in a numeric field


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?
Newbie
 
Join Date: Mar 2008
Posts: 4
#5: Mar 9 '08

re: Has the user entered text in a numeric field


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?


Expand|Select|Wrap|Line Numbers
  1.  
  2. <script language="Javascript">
  3.       function chk_val(obj) {
  4.  
  5.           var chk = /^\d+$/.test(obj.value);
  6.  
  7.  
  8.  
  9.           if (!chk) {
  10.  
  11.               location.href = 'SuburbSearch.php';
  12.  
  13.               obj.focus();
  14.  
  15.           }
  16.  
  17.       }
  18. </script>
  19. <body>
  20.  
  21. <h1>Start Here</h1>
  22.  
  23. <form action="PostcodeSearch.php" method="post">
  24.  
  25.  
  26. Enter Postcode:<br>
  27.  
  28. <input name="searchterm" type=text value="" onblur="chk_val(this);">
  29.  
  30. <br>
  31. </form>
  32.  
  33.  
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Mar 9 '08

re: Has the user entered text in a numeric field


Instead of changing the location.href, change the form's action property and then call its submit() method.
Newbie
 
Join Date: Mar 2008
Location: Las Palmas de GC
Posts: 10
#7: Mar 9 '08

re: Has the user entered text in a numeric field


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.)
Newbie
 
Join Date: Mar 2008
Posts: 4
#8: Mar 9 '08

re: Has the user entered text in a numeric field


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!
Newbie
 
Join Date: Oct 2009
Posts: 1
#9: 3 Weeks Ago

re: Has the user entered text in a numeric field


Expand|Select|Wrap|Line Numbers
  1. function chk_val(i) {
  2.   var chk= parseInt(i);
  3.   if(chk==NaN) { return false; } else { return true; }
  4. };
  5.  
  6.  
  7. var n=  chk_val(userInput);
  8.  
  9. if(n) { 
  10. // allow user to do xyz
  11. } else {
  12. // not a number! do something else.
  13. }
Reply