473,569 Members | 2,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

validate input field for numeric value

idsanjeev
241 New Member
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 6629
acoder
16,027 Recognized Expert Moderator MVP
What have you attempted so far?
Jul 30 '08 #2
idsanjeev
241 New Member
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 Recognized Expert Moderator MVP
Either use parseFloat and isNaN or use a simple float regular expression, e.g. /^\d{2}\.\d{2}$/
Jul 30 '08 #4
idsanjeev
241 New Member
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 New Member
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="ret urn numbersonly(thi s, event,true)"


hope usefull,

kind regards, maminx
Aug 1 '08 #6
gits
5,390 Recognized Expert Moderator Expert
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="ret urn numbersonly(thi s, 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 Recognized Expert Moderator MVP
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 Recognized Expert Moderator MVP
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
2108
by: Hernán Castelo | last post by:
hi should i validate cookies values? thanks -- atte, Hernán Castelo SGA - UTN - FRBA
2
1877
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 not "", then either the ref names has to be filled in OR the res date has to be filled in. I have tried many combinations of this. Any suggestions...
7
6742
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 (!isNumeric(document.frmA.f1.value)) { alert("Please fill in Numeric")document.frmA.f1.value="" document.frmA.f1.focus() return false; }
2
3091
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 I can have JS switch from one input field to another based on me clicking in the empty box. Here is my code: ########## Start of code ...
11
8142
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 to validate 3 types of fields: text, radio button, dropdown menu. but the code doesn't validate. After 2 days, it's time I asked for guidence....
1
3980
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. Depends on the pull down list selection, I use script.aculo.us to validate the user input before submit and pass the necessary data, such as contact...
1
2827
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 am working with is using .asp. Their are 30 multiple choice questions. Each will have have 3 or 4 checkboxes where the test taker will choose only...
0
1390
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 valid input before inserting/updating? Test the length of a varchar/char field? Validate an int field as valid numeric input (no alpha) ... and so...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7922
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6281
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5218
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.