473,395 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

validate input field for numeric value

idsanjeev
241 100+
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
8 6621
acoder
16,027 Expert Mod 8TB
What have you attempted so far?
Jul 30 '08 #2
idsanjeev
241 100+
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
16,027 Expert Mod 8TB
Either use parseFloat and isNaN or use a simple float regular expression, e.g. /^\d{2}\.\d{2}$/
Jul 30 '08 #4
idsanjeev
241 100+
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
maminx
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
5,390 Expert Mod 4TB
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
16,027 Expert Mod 8TB
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
16,027 Expert Mod 8TB
i'm using this library below, u can use this

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

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

Similar topics

4
by: Hernán Castelo | last post by:
hi should i validate cookies values? thanks -- atte, Hernán Castelo SGA - UTN - FRBA
2
by: Meredith | last post by:
I am using a script to validate a form using the presence of a value in one field and determine if there is a value in one of two fields. It is an either/or situation. If the date rcvd field is...
7
by: juicy | last post by:
I done a numeric field checking as below, but I found that if I insert float number (10.01), it validate it as non numeric and return false.. What is going wrong? if...
2
by: Cranky | last post by:
Ok, here is my scenario: I need to input numbers using my handheld IPAQ. I figured out how to create an online numeric keypad for inputting numbers into an input field, what I need to know is how...
11
by: TokyoJ | last post by:
I run a small camp in Alaska for kids and my director is asking for a web form. Could someone please have a look and offer some advice on where I'm making mistake(s)? I'm using the RegExp function...
1
by: SkipNRun | last post by:
I am a novice when comes to JavaScript, AJAX. I am working on a form, which will allow users to update their contact information. In order to make the form flexible, I need to use pull down list. ...
1
by: mbarnhizer | last post by:
Hello All, Trying to figure out how to validate a series of questions on an online test. I am thinking that VB or Javascript is the best route, but your input may make a difference. The site i...
0
by: =?Utf-8?B?YyMganVua2ll?= | last post by:
Title says it ... my C# web application uses a number of FormViews that are inserted/updated/deleted via stored procedures. Is there a way thru C# coding (example?) to validate a date field as...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.