473,387 Members | 1,942 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,387 software developers and data experts.

text box validation

hi
thank you very much for replying quickly if i could do anything tell me
im new to javascript so i have problem with validating text box to accept only positive numbers and not more than 1000

thanks
Apr 2 '09 #1
8 2035
acoder
16,027 Expert Mod 8TB
You could use a regular expression:
Expand|Select|Wrap|Line Numbers
  1. /^\d+$/
to test that there are only integers. Once you have that established, use parseInt to convert the string into an integer and test that it's less than 1000:
Expand|Select|Wrap|Line Numbers
  1. if (parseInt(str) > 1000) {
  2.    alert("Error ...");
  3. }
Apr 2 '09 #2
sorry but i dont know regular expression and i try everything didnt work
Apr 2 '09 #3
acoder
16,027 Expert Mod 8TB
See this and this to get acquainted. If you don't want to use a regular expression, you could create isDigit()/isInteger()/isPositiveInteger() functions, e.g.
Expand|Select|Wrap|Line Numbers
  1. function isDigit(num) {
  2.     if (num.length > 1) return false;
  3.     var str="1234567890";
  4.     return (str.indexOf(num) != -1);
  5. }
Apr 2 '09 #4
its not working i dont why can use isnan function if yes how
Apr 3 '09 #5
acoder
16,027 Expert Mod 8TB
isNaN wouldn't be enough. The code I posted for isDigit should be used to test one character. Test all characters to test if the string is a positive integer, e.g.
Expand|Select|Wrap|Line Numbers
  1. function isInteger(num) {
  2.     for (i = 0; i < num.length; i++) {
  3.         var char = num.charAt(i);
  4.         if (!isDigit(char)) return false;
  5.     }
  6.     return true;
  7. }
Apr 3 '09 #6
this is the script i have but i want to add the intger shouldnt be more than 100 ie between 0-100
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. <!--
  3. function checkNum(number)
  4. {
  5. if (isNaN(number.value) )
  6.   {
  7.     alert(number.value + " is not a number. Please re-enter.");
  8.     number.focus();
  9.     number.select();
  10.   }
  11.   return;
  12. }
Apr 3 '09 #7
i have this code but i want to put more than 1000 is not accepted
Expand|Select|Wrap|Line Numbers
  1. function checkNum(number)
  2. if (isNaN(number.value) )
  3.   {
  4.     alert(number.value + " is not a number. Please re-enter.");
  5.     number.focus();
  6.     number.select();
  7.   }
  8.  
  9.   return;
  10. }
Apr 4 '09 #8
acoder
16,027 Expert Mod 8TB
isNaN will be false for float numbers too, e.g. 1.23, but you only want positive integers.

Anyway, to check if the number is not greater than 1000, see post #2.
Apr 4 '09 #9

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

Similar topics

5
by: Otto Krüse | last post by:
Hi everyone, I'm building a GUI in which I want, amongst other things, for people to fill in there postal code. The postal codes of my country (Holland) are in this format: 1234 AB So for the...
1
by: cga1982 | last post by:
I have a text box (for a password) with a validation rule that checks this password against another text box on the form and either lets you continue or tells you that the password is incorrect and...
4
by: Fraggle | last post by:
Hi, I want to validate a text box, The user can leave it blank, or fill it in. If they fill it in then it must be a date within a certain range. How can I achieve this? Thank you very much
6
by: The Eeediot | last post by:
Hello, Folks... I'm almost becoming a regular to this newsgroup. I am trying to display the contents of an MS-SQL Text field to a TextBox in ASPdotNET. The text in this field contains all...
1
by: Stephen Adam | last post by:
Hi there, I have written a custom validation control which checks to see of an input field is not empty and contains only numeric data. I was using a regular expression validation control but...
1
by: Kris | last post by:
Hi, I have a DataGrid where in each row has couple of text boxes and an update button. Each row is dynamically generated as the number of rows are not known ahead of time. When the user clicks the...
1
by: Joel Barsotti | last post by:
Is there anything builtin to ASP.net that allows you to tie a text box to a button so when you press enter in the text box it emulates clicking a near by button. I've coded up some client side...
9
by: sellcraig | last post by:
Microsoft access 2 tables table "data main" contains a field called "code" table "ddw1" is created from a make table query of "data main" Goal- the data in "code" field in needs to...
3
by: den | last post by:
inner a text element if I want to allow the insertion of only: alphabet's letters a,b,c,.... and A,B,C,... number and this - and this _ and not want space blank and others characters what is...
2
by: devnew | last post by:
hi i am new to tkinter and would like some help with canvas i am doing validation of contents of a folder and need to show the ok/error messages on a canvas resultdisplay =Canvas(...)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...

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.