By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,434 Members | 2,884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,434 IT Pros & Developers. It's quick & easy.

code to find whether only numerals are entered in a text box

P: 10
Hi ,


Can anybody help me with the code , to find whether only numerals are entered in a text box (using js.),at the time of saving.

jk
Jan 7 '08 #1
Share this Question
Share on Google+
7 Replies


Expert 100+
P: 785
Hi ,


Can anybody help me with the code , to find whether only numerals are entered in a text box (using js.),at the time of saving.

jk
Just put an onKeyUp-event and onChange-event for the text box which calls a javascript function.
Or call this function in your onSubmit() function of your form.
This javascript function, which you can put into your ".js" file, uses a regular expression that checks for digits.
If you do it this way with regExpr., then you can write the function by using only one line of source code.
Jan 7 '08 #2

gits
Expert Mod 5K+
P: 5,333
hi ...

a regExp would do the job. in case you only want integers you may use something like the following for it:

Expand|Select|Wrap|Line Numbers
  1. // val may be you input-value
  2. var val = '123';
  3.  
  4. // function that checks for numbers - returns true when 
  5. // ok otherwise it returns false (the regExp is literally created
  6. // with /^\d+$/ and checks for numbers from start to end)
  7. function test_value(val) {
  8.     return /^\d+$/.test(val);
  9. }
  10.  
  11. // now we could call the function
  12. alert(test_value(val));
  13.  
kind regards
Jan 7 '08 #3

P: 10
hi frnds,

thanx for ur fast reply! i hv done tat as u said and itz workin!!!

gitz,actually i want to check whether only integers are entered in the field and if there is notin other than integers,then i hv to reject it!I hv done tat using the code u hv given.
Then i need a thing similar to this,tat is i want to check whether only special characters are entered in a textfield?
can u help me in tat?


thanks in advace
jk
Jan 7 '08 #4

gits
Expert Mod 5K+
P: 5,333
:) glad to hear that ... for further information you may have a look here
and may be for your next purpose you may use:

Expand|Select|Wrap|Line Numbers
  1. /^\W$/
instead of

Expand|Select|Wrap|Line Numbers
  1. /^\d$/
kind regards
Jan 7 '08 #5

P: 10
Hi gits,

Thanks again ...by replacing the function as u said,itz now possible to check the existence of special characters only in a textbox.!!!

Takecare!




jk
Jan 8 '08 #6

gits
Expert Mod 5K+
P: 5,333
hi ...

that's what you needed ... in't it :) ... post back to the forum anytime you have more questions ...

kind regards
Jan 8 '08 #7

P: 10
hi ...

that's what you needed ... in't it :) ... post back to the forum anytime you have more questions ...

kind regards

hi gits,

Yes thatz exactly wat i needed! Then i ll post my queries in dis forum,when i got any problems.


WarmRegards
jk
Jan 8 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.