javascript ?

  • akaash
  • Beginner
  • Beginner
  • akaash
  • Posts: 62
  • Loc: Gujurat,India

Post 3+ Months Ago

there is a text box in my form tag .

I want to validate that a user enters only a number between 1 and max.number where max.number is a variable.

if the user enters wrong thing then alert him.

I want a function in javascript that is written in head and taking 2 parameters;
one: the user entered value
and
second: max.number.

how can i do that in javascript?
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

Code: [ Select ]
if(usrVal>=1 && usrVal<=maxNumber){
  //do the stuff that you wanna do
}else{
  alert('you suck');
}
  1. if(usrVal>=1 && usrVal<=maxNumber){
  2.   //do the stuff that you wanna do
  3. }else{
  4.   alert('you suck');
  5. }


assuming the you want between 1 and max number inclusive.

you may also have to convert the input text to a number first, I cannot remember if Jscript does this automagically.


//edit, if you do need to convert datatypes, the best bet is probably to use:
Code: [ Select ]
usrVal=parseFloat(usrVal);
if(usrVal>=1 && usrVal<=maxNumber){
  //do the stuff that you wanna do
}else{
  alert('you suck');
}
  1. usrVal=parseFloat(usrVal);
  2. if(usrVal>=1 && usrVal<=maxNumber){
  3.   //do the stuff that you wanna do
  4. }else{
  5.   alert('you suck');
  6. }


To check out how parseFoat() works then check this link:
http://www.javascripter.net/faq/convert2.htm

I've used parseFloat() because it is superior to parseInt() in that it will allow decimals, not just integers, and it should strip away some of the crap that users might enter.
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Posts: 1098

Post 3+ Months Ago

Try this:
.c


Code: [ Select ]
<form NAME="myform" onSubmit="return validate(this);">
<input TYPE="text" NAME="mynumber" VALUE="">
<input TYPE="submit" NAME="submit" VALUE="submit">
</form>

<script LANGUAGE="JavaScript" TYPE="text/javascript">
function validate(form_obj){
 mynumber = form_obj.mynumber.value * 1;
 mymax = 100;
 mymin = 0;
 if(mynumber > mymax || mynumber < mymin){
  alert("Sorry, your number is out of range:" + mymin + " to " + mymax);
 }
 else {return true;}
}
</script>
  1. <form NAME="myform" onSubmit="return validate(this);">
  2. <input TYPE="text" NAME="mynumber" VALUE="">
  3. <input TYPE="submit" NAME="submit" VALUE="submit">
  4. </form>
  5. <script LANGUAGE="JavaScript" TYPE="text/javascript">
  6. function validate(form_obj){
  7.  mynumber = form_obj.mynumber.value * 1;
  8.  mymax = 100;
  9.  mymin = 0;
  10.  if(mynumber > mymax || mynumber < mymin){
  11.   alert("Sorry, your number is out of range:" + mymin + " to " + mymax);
  12.  }
  13.  else {return true;}
  14. }
  15. </script>
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

Carnix: if someone enters "17 " with the space in it, yours throws an error. Also if they enter 0.1, this counts as valid input, which it isn't.....
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Posts: 1098

Post 3+ Months Ago

Yeah, I didn't throw in any other validation to make sure the input can be converted into a number.

You're right, there should first be a check that it's not a string ("hello" * 1 will error as well...)

However, 0.1 is greater than 0, so it is valid input for that... After looking at the original post again, I see my min was incorrectly set at 0 not 1...

Ok... keeping me honest, I see... in THAT case... try this one:

Code: [ Select ]
<html>
<script LANGUAGE="JavaScript" TYPE="text/javascript">
function validate(form_obj){
 mynumber = form_obj.mynumber.value;
 if(isNumeric(mynumber)){
  mymax = 100;
  mymin = 1;
  if(mynumber > mymax || mynumber < mymin){
  alert("Sorry, your number is out of range:" + mymin + " to " + mymax);
  }
  else {return true;}
 }
 else{
  alert("Please enter numbers only");
  return false;
 }
}


function isNumeric(input){
 characters = "0123456789.";
 response = true;
 for(i=0;(i<input.length && response);i++){
  if(characters.indexOf(input.charAt(i)) == -1){ response = false; }
 }
 return response;
}
</script>

<form METHOD="post" NAME="myform" onSubmit="return validate(this);">
<input TYPE="text" NAME="mynumber" VALUE="">
<input TYPE="submit" NAME="submit" VALUE="submit">
</form>
</html>
  1. <html>
  2. <script LANGUAGE="JavaScript" TYPE="text/javascript">
  3. function validate(form_obj){
  4.  mynumber = form_obj.mynumber.value;
  5.  if(isNumeric(mynumber)){
  6.   mymax = 100;
  7.   mymin = 1;
  8.   if(mynumber > mymax || mynumber < mymin){
  9.   alert("Sorry, your number is out of range:" + mymin + " to " + mymax);
  10.   }
  11.   else {return true;}
  12.  }
  13.  else{
  14.   alert("Please enter numbers only");
  15.   return false;
  16.  }
  17. }
  18. function isNumeric(input){
  19.  characters = "0123456789.";
  20.  response = true;
  21.  for(i=0;(i<input.length && response);i++){
  22.   if(characters.indexOf(input.charAt(i)) == -1){ response = false; }
  23.  }
  24.  return response;
  25. }
  26. </script>
  27. <form METHOD="post" NAME="myform" onSubmit="return validate(this);">
  28. <input TYPE="text" NAME="mynumber" VALUE="">
  29. <input TYPE="submit" NAME="submit" VALUE="submit">
  30. </form>
  31. </html>
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

It's always good practice to write your own functions, but if you don't want to reinvent the wheel there are some good open source examples out there. One that I have used in the past is FormChek.js which has functions for such things as isInteger, isSignedInteger, isFloat etc. It also has functions for Soc. Sec. numbers, email validation, cc cards etc. There is an overview page here. The overview page has examples of US and International usage.
  • akaash
  • Beginner
  • Beginner
  • akaash
  • Posts: 62
  • Loc: Gujurat,India

Post 3+ Months Ago

thanks a lot for ur responses..

they are just great...

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 63 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.