473,671 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validate Numeric fields

5 New Member
Hiya,

Does anyone know any simple JScript code to validate a numeric field and also the length of the data that should be entered into the field.

e.g. 12345 - the user can only enter numeric fields and the lenght of the value should be five (no more and no less)

Thanks
Mar 11 '08 #1
4 2691
vee10
141 New Member
Hi,

u can try out this

Expand|Select|Wrap|Line Numbers
  1.  
  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head id="Head1" runat="server">
  4.     <title>Untitled Page</title>
  5.     <script type="text/javascript">
  6.     function chekc()
  7.     {   
  8.  
  9.     var str = document.getElementById("text1").value;    
  10.     alert(str.length);
  11.     var valid="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  12.     for(i=0;i<str.length;i++)
  13.     {
  14.     if(valid.indexOf(str.charAt(i)) ==-1)
  15.     alert("number");
  16.     else
  17.     alert("not a number");
  18.     }
  19.  
  20.     }
  21.  
  22.     </script>
  23. </head>
  24. <body>
  25.     <form id="form1" runat="server">
  26.     <div>
  27.    <input type="text" id="text1" onchange="chekc()"/>
  28.     </div>
  29.     </form>
  30. </body>
  31. </html>
  32.  

Hiya,

Does anyone know any simple JScript code to validate a numeric field and also the length of the data that should be entered into the field.

e.g. 12345 - the user can only enter numeric fields and the lenght of the value should be five (no more and no less)

Thanks
Mar 11 '08 #2
somuchbetta
5 New Member
Hi,

u can try out this

Expand|Select|Wrap|Line Numbers
  1.  
  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head id="Head1" runat="server">
  4.     <title>Untitled Page</title>
  5.     <script type="text/javascript">
  6.     function chekc()
  7.     {   
  8.  
  9.     var str = document.getElementById("text1").value;    
  10.     alert(str.length);
  11.     var valid="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  12.     for(i=0;i<str.length;i++)
  13.     {
  14.     if(valid.indexOf(str.charAt(i)) ==-1)
  15.     alert("number");
  16.     else
  17.     alert("not a number");
  18.     }
  19.  
  20.     }
  21.  
  22.     </script>
  23. </head>
  24. <body>
  25.     <form id="form1" runat="server">
  26.     <div>
  27.    <input type="text" id="text1" onchange="chekc()"/>
  28.     </div>
  29.     </form>
  30. </body>
  31. </html>
  32.  

Thanks for the quick reply - the code seems very advanced and im finding it hard to understand it e.g i have no clue what runat="server" means :-( ... and the problem is i need to explain it when im presenting my website.

At the moment i am only able to check that data has been entered into the field

[HTML]<script type="text/javascript">
<!--
function validate(regfor m) {
//Perform validation checks

//Check that telephone number exsit in this field
if (regform.telno. value=="") {
alert("Please complete the field: Contact number");
regform.telno.f ocus();
return false;
}
return true;
}
//-->
</script>


<form method="get" name="regform" action="regcomp lete.asp" onsubmit="retur n validate(this); ">
Contact Number:<input type="text" name="telno" size="15" />
<input type="submit" value="submit">
</form>[/HTML]
Mar 11 '08 #3
vee10
141 New Member
Hey
u no need to bother abt the runat ="server" since i am using dot 2.0 i wil get that u just check out the below code if u have any doubt in that code u can contact

Expand|Select|Wrap|Line Numbers
  1. function validate(regform)
  2.     {   
  3.      //Check that telephone number exsit in this field
  4. if (regform.telno.value=="") {
  5. alert("Please complete the field: Contact number");
  6. regform.telno.focus();
  7. return false;
  8. }
  9.     var str = regform.telno.value;    
  10.     alert(str.length);
  11.     var valid="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  12.  
  13.     for(i=0;i<str.length;i++)
  14.     {
  15.     if(valid.indexOf(str.charAt(i)) !=-1)    
  16.  
  17.     {
  18.     alert("Please enter numeric");
  19.     return false;
  20.     }
  21.  
  22.     }
  23.     return true;
  24.  
  25.     }
  26.  


Thanks for the quick reply - the code seems very advanced and im finding it hard to understand it e.g i have no clue what runat="server" means :-( ... and the problem is i need to explain it when im presenting my website.

At the moment i am only able to check that data has been entered into the field

<script type="text/javascript">
<!--
function validate(regfor m) {
//Perform validation checks

//Check that telephone number exsit in this field
if (regform.telno. value=="") {
alert("Please complete the field: Contact number");
regform.telno.f ocus();
return false;
}
return true;
}
//-->
</script>


<form method="get" name="regform" action="regcomp lete.asp" onsubmit="retur n validate(this); ">
Contact Number:<input type="text" name="telno" size="15" />
<input type="submit" value="submit">
</form>
Mar 12 '08 #4
gits
5,390 Recognized Expert Moderator Expert
hi ...

just a note :) ... there are some shorter ways to accomplish that task:

Expand|Select|Wrap|Line Numbers
  1. // 1. example - uses isNaN-method
  2. function validate_num_vals() {
  3.     var val = document.getElementById('your_text_node_id').value;
  4.     var ret = !isNaN(val);
  5.  
  6.     if (!ret) {
  7.         alert('enter a number');
  8.     }
  9.  
  10.     return ret;
  11. }
  12.  
  13. // 2. example - uses regExp
  14. function validate_num_vals() {
  15.     var val = document.getElementById('your_text_node_id').value;
  16.     var ret = /^\d+$/.test(val);
  17.  
  18.     if (!ret) {
  19.         alert('enter a number');
  20.     }
  21.  
  22.     return ret;
  23. }
  24.  
kind regards
Mar 12 '08 #5

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

Similar topics

9
2701
by: varois83 | last post by:
Hi Newbie here. I have been working on creating a guestbook for my site as practice and am learning a lot. Do you guys validate your forms first on the client with javascript and then on the server with PHP or just use one of the two and if yes which one? I don't want to reinvent the wheel too much. Thanks a lot
5
4251
by: Rob Wahmann | last post by:
Not sure if my first message went through because of the attachment... I'm currently using formchek.js to validate various form fields, however, I cannot figure out how to validate a field with a numeric range of 6-10 digits. I apologize in advance for asking such a question since I'm using formchek.js, but my strength is server-side programming not JavaScript. I appreciate any tips or advice you can provide! TIA - Rob
6
1818
by: Peter Afonin | last post by:
Hello, Should be a pretty simple question: I need to validate a string. It must be numeric and contain exactly 12 characters. I know how to do it in code, but I'd like to use a validation control. Should I use a Regular expression validator? What expression should I use? I checked http://www.regxlib.com, but couldn't find exactly what I need.
4
4278
by: Wysiwyg | last post by:
I need to validate a form to ensure that all of the fields add up correctly. I can't do this while the user is entering data since validation needs to be done after the entry is completed. What's the "best" way to validate prior to submitting? I could add an onsubmit attribute to the form which executes the validation in Javascript. That way the response doesn't need to be sent before validation takes place. Is this pretty much the way...
7
6744
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; }
5
1227
by: UJ | last post by:
I have two fields where the user is entering a length of time (minutes and seconds) and need to validate it so that a) they are both numbers. b) If the minutes is 0, the seconds has to be > 10. Is there an easy way to do this on the client side? TIA - Jeff.
3
2469
by: shyamg | last post by:
hi all, This javascript is working IE but not working in FIreFox, validating text fields. var dealerid = new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890 ','Alpha-numeric input only.'); var dealinit = new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890 ','Alpha-numeric input only.'); var dealername = new keybEdit('abcdefghijklmnopqurstuvwxyz ','Alphabets input only.'); var rank = new...
5
2562
nathj
by: nathj | last post by:
Hi, I have been looking around the forum and the web for a way to achieve this and so far I have drawn a blank. So I head to the forum as I'm sure someone knows how to do this. I have a form which I validate as the user enters data, this is straight forward enough. Part of the validation ensures that all mandatory fields are entered before the continue button is enabled. (unfortunately this is a development site at present and so I can't...
0
1409
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 on? I would like to do this validation before the stored procedures fire (perhaps stopping), of...
0
8476
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8393
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8820
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7433
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6223
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5695
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4224
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2810
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
2
2051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.