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

validate input field for numeric value

idsanjeev
100+
P: 241
I try to validate inpute field for weight that accept only numeric value like 65.12 not allow any special character or character but allow '.' .
thanks
jha
Jul 30 '08 #1
Share this Question
Share on Google+
8 Replies


acoder
Expert Mod 15k+
P: 16,027
What have you attempted so far?
Jul 30 '08 #2

idsanjeev
100+
P: 241
What have you attempted so far?
i am using this code for validation but this way only can check null,(-)value,and character with special caracter. but don't know how can do with my requirement like 45.10

Expand|Select|Wrap|Line Numbers
  1. function ck_wt(obj)
  2. {
  3. if(obj.value=="")
  4. {
  5. obj.focus();
  6. alert("Wt. Must be entered.")
  7. }
  8. else if(obj.value<0 || obj.value=="")
  9. {
  10. obj.focus();
  11. alert("wait must be +ve")
  12. return;
  13. }
  14.   else if(!obj.value.match(/^\d+$/)){
  15.     obj.focus();
  16.     alert("Only numbers are allowed.");
  17.     return;
  18.   }
  19. }
thanks
jha
Jul 30 '08 #3

acoder
Expert Mod 15k+
P: 16,027
Either use parseFloat and isNaN or use a simple float regular expression, e.g. /^\d{2}\.\d{2}$/
Jul 30 '08 #4

idsanjeev
100+
P: 241
Either use parseFloat and isNaN or use a simple float regular expression, e.g. /^\d{2}\.\d{2}$/
the code for check wait is modified by but not work it accepted special character like 4.5a or 4.5;aq but i wants to 4.5 only or 4

Expand|Select|Wrap|Line Numbers
  1. function ck_wt(obj)
  2. {
  3. if(obj.value=="")
  4. {
  5. obj.focus();
  6. alert("Wt. Must be entered.")
  7. }
  8. else if(obj.value<0 || obj.value=="")
  9. {
  10. obj.focus();
  11. alert("wait must be +ve")
  12. }
  13. else if(obj.value.match(/^\d{2}\.\d{2}$/))
  14. {
  15. obj.focus();
  16. alert ("testing")
  17. return;
  18. }
jha
Aug 1 '08 #5

P: 77
the code for check wait is modified by but not work it accepted special character like 4.5a or 4.5;aq but i wants to 4.5 only or 4

Expand|Select|Wrap|Line Numbers
  1. function ck_wt(obj)
  2. {
  3. if(obj.value=="")
  4. {
  5. obj.focus();
  6. alert("Wt. Must be entered.")
  7. }
  8. else if(obj.value<0 || obj.value=="")
  9. {
  10. obj.focus();
  11. alert("wait must be +ve")
  12. }
  13. else if(obj.value.match(/^\d{2}\.\d{2}$/))
  14. {
  15. obj.focus();
  16. alert ("testing")
  17. return;
  18. }
jha
i'm using this library below, u can use this

Expand|Select|Wrap|Line Numbers
  1. function numbersonly(myfield, e, dec) {
  2.     var key;
  3.     var keychar;
  4.  
  5.     if (window.event)
  6.        key = window.event.keyCode;
  7.     else if (e)
  8.        key = e.which;
  9.     else
  10.        return true;
  11.     keychar = String.fromCharCode(key);
  12.  
  13.     // control keys
  14.     if ((key==null) || (key==0) || (key==8) || 
  15.         (key==9) || (key==13) || (key==27) )
  16.        return true;
  17.  
  18.     // numbers
  19.     else if ((("-0123456789").indexOf(keychar) > -1))
  20.     //else if ((("0123456789")))
  21.        return true;
  22.  
  23.     // decimal point jump
  24.     else if (dec && (keychar == "."))
  25.        {
  26.        myfield.form.elements[dec].focus();
  27.        return false;
  28.        }
  29.     else
  30.        return false;
  31. }

and to call that function in HTML form is with this event below :

onkeypress="return numbersonly(this, event,true)"


hope usefull,

kind regards, maminx
Aug 1 '08 #6

gits
Expert Mod 5K+
P: 5,343
i'm using this library below, u can use this

Expand|Select|Wrap|Line Numbers
  1. function numbersonly(myfield, e, dec) {
  2.     var key;
  3.     var keychar;
  4.  
  5.     if (window.event)
  6.        key = window.event.keyCode;
  7.     else if (e)
  8.        key = e.which;
  9.     else
  10.        return true;
  11.     keychar = String.fromCharCode(key);
  12.  
  13.     // control keys
  14.     if ((key==null) || (key==0) || (key==8) || 
  15.         (key==9) || (key==13) || (key==27) )
  16.        return true;
  17.  
  18.     // numbers
  19.     else if ((("-0123456789").indexOf(keychar) > -1))
  20.     //else if ((("0123456789")))
  21.        return true;
  22.  
  23.     // decimal point jump
  24.     else if (dec && (keychar == "."))
  25.        {
  26.        myfield.form.elements[dec].focus();
  27.        return false;
  28.        }
  29.     else
  30.        return false;
  31. }

and to call that function in HTML form is with this event below :

onkeypress="return numbersonly(this, event,true)"


hope usefull,

kind regards, maminx
@maminx: according to the posting guidelines USE THE CODE TAGS ... i'm getting tyred fixing every single post by you with that ...

regards,
MOD
Aug 1 '08 #7

acoder
Expert Mod 15k+
P: 16,027
the code for check wait is modified by but not work it accepted special character like 4.5a or 4.5;aq but i wants to 4.5 only or 4
Oh, I see you want any type of float number. What I suggested earlier was a very simple version from what you posted, but it seems you need something a little more complete. This link should help.
Aug 1 '08 #8

acoder
Expert Mod 15k+
P: 16,027
i'm using this library below, u can use this

...
It allows multiple "." decimal characters.
Aug 1 '08 #9

Post your reply

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