Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reset textbox and validation

Status
Not open for further replies.

dkin

Programmer
Dec 11, 2002
39
ES
Hi,

The enclosed JavaScript below checks an input range. How can I also reset the input in the text box if it is not value range?



<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function checkRange(numValue,numLow,numHigh){
if(isNaN(numValue)){
alert(&quot;Please enter a valid number&quot;);
return false;
}else{
if(parseFloat(numValue)< numLow || parseFloat(numValue)>numHigh){
alert(&quot;Please enter a value between &quot; + numLow + &quot; and &quot; + numHigh);
return false;
}
}
return true;

}
//-->
</SCRIPT>
</HEAD>
<BODY>

<FORM action=&quot;&quot; method=POST id=form1 name=form1>
<INPUT type=&quot;text&quot; id=text1 name=text1 onChange=&quot;javascript:checkRange(this.value,10,500)&quot;>
</FORM>
</BODY>
</HTML>
 
you have a few choices here but below is what I think would be the easiest for what you already have.
seeing as you are using parsefloat you are converting the value to a integer thus causing the typical value == &quot;&quot;
to not work seeing as that is string orientated. so, the solution is to do = &quot;&quot;.
you then should change the parameter being passed as the object and not the value of the field. this will give you far more availability to the text field,
changes are in bold
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function checkRange(numValue,numLow,numHigh){
if(isNaN(numValue.value)){
alert(&quot;Please enter a valid number&quot;);
return false;
}else{
if(parseFloat(numValue.value)< numLow || parseFloat(numValue.value)>numHigh){
alert(&quot;Please enter a value between &quot; + numLow + &quot; and &quot; + numHigh);
numValue.value=&quot;&quot;;
return false;
}
}
return true;

}
//-->
</SCRIPT>
</HEAD>
<BODY>

<FORM action=&quot;&quot; method=POST id=form1 name=form1>
<INPUT type=&quot;text&quot; id=text1 name=text1 onChange=&quot;return checkRange(this,10,500)&quot;>
</FORM>
</BODY>
</HTML>
---------------------------------------
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }
---------------------------------------
for the best results to your questions: FAQ333-2924

 
I have tried it but it does not work, I do not know why.
 
seems to that already.. atleast in ie... could you add though

formName.textFieldName.value=&quot;&quot;;
 
I am lost and can not solve/make it work it?, simple scripts are also welcome )-:

<SCRIPT LANGUAGE=javascript>
<!--
function checkRange(numValue,numLow,numHigh){
if(isNaN(numValue.value)){
alert(&quot;Please enter a valid number&quot;);
return false;
}else{
if(parseFloat(numValue.value)< numLow || parseFloat(numValue.value)>numHigh){
alert(&quot;Please enter a value between &quot; + numLow + &quot; and &quot; + numHigh);
formName.textFieldName.value=&quot;&quot;;
return false;
}
}
return true;

}
//-->
 
what browser are you testing on.
the script I gave works fine in IE 5.5 NN 6.2
is there more to it then you are showing that may be causing some other problems ---------------------------------------
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }
---------------------------------------
for the best results to your questions: FAQ333-2924

 
BTW
you were suppose to fill in the blanks here
formName.textFieldName.value=&quot;&quot;;
equals
form1.text1.value ---------------------------------------
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }
---------------------------------------
for the best results to your questions: FAQ333-2924

 
It is a very big PHP file but thats the only javascript.
 
change
formName.textFieldName.value=&quot;&quot;;
to
form1.text1.value=&quot;&quot;;

and see what you get
 
Change it, and it still do not want to work.

May be the problem is the location of the JS, structure look like this:
<html>
<head>
<title> </title>
<SCRIPT LANGUAGE=javascript>
<!--
function checkRange(numValue,numLow,numHigh){
if(isNaN(numValue.value)){
alert(&quot;Please enter a valid number&quot;);
return false;
}else{
if(parseFloat(numValue.value)< numLow || parseFloat(numValue.value)>numHigh){
alert(&quot;Please enter a value between &quot; + numLow + &quot; and &quot; + numHigh);
form1.text1.value=&quot;&quot;;
return false;
}
}
return true;

}
//-->
</script>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body>
<form name=form1>
 
must be a browser issue... it worked fine for me the way you had it before... are you getting an error or is the test field simply not being reset...

If it is simply not being reset then it is time to kick into troubleshooting mode- trim down your code and get rid of everything until you are simply trying to reset the textfield to blank- once that is working then slowly add back in a snippet of code at a time until it breaks again- then you will have found your trouble area...
 
going off of your first post of the code and you asking for the easiest way to do things even knowing my first example is the easiest way to do it off your formatting, to make this as simple as possible what you want to do is not pass any parameters and point to the form names in the script.
want to make as simple as possible, then this is as simple as possible
if this doesn't work then you have other problems and or need to check browser settings for javascript enabled etc..
either that or you are running something like netscape 1 and are not telling us. [wink]
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function checkRange(){
var num1 = 10;
var num2 = 500;
var numValue = document.form1.text1.value;
if(isNaN(numValue)){
alert(&quot;Please enter a valid number&quot;);
return false;
}
else
{
if(parseFloat(numValue) < num1 || parseFloat(numValue) > num2){
alert(&quot;Please enter a value between &quot; + num1 + &quot; and &quot; + num2);

document.form1.text1.value = &quot;&quot;
return false;
}
}
return true;

}
//-->
</SCRIPT>
</HEAD>
<BODY>

<FORM action=&quot;blah.asp&quot; method=POST id=form1 name=form1>
<INPUT type=&quot;text&quot; id=text1 name=text1 onChange=&quot;javascript:checkRange()&quot;>
</FORM>
</BODY>
</HTML>

BTW that meta tag should not be in that place in the page in that other example. ---------------------------------------
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }
---------------------------------------
for the best results to your questions: FAQ333-2924

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top