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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate form fields for specific values ( not a range) 1

Status
Not open for further replies.

fskipper

Programmer
Feb 26, 2004
10
US
I am trying to write a script to make sure that the user enters specific values ( 0.5,1,3,5, or 10) or else get an alert box. I have a script for another field that validates on a range, but I can't seem to modify it to work for *specific* values. Below is the range script, which works fine for a range. The function is called onBlur of the field with parameters passed in the onBlur event.

function valuevalidation(entered, min, max, alertbox, datatype){

with (entered)
{
checkvalue=parseFloat(value);
if (datatype)
{smalldatatype=datatype.toLowerCase();
if (smalldatatype.charAt(0)=="i") {checkvalue=parseInt(value)};
}
if ((parseFloat(min)==min && checkvalue<min) || (parseFloat(max)==max && checkvalue>max) || value!=checkvalue)
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}

//This is the onBlur event:
/* <input name="P" type="text" onBlur="valuevalidation(this, 1, 5,'Value MUST be an Integer in the range: 1-5');" onChange="add();" size="2">
*/
 
When there is such a small list of possible choices for a user to choose from, why not just use a pulldown instead of making them type in the value? There is 0 chance for error that way.
Code:
<select name=blahSelect>
<option value=.5>0.5
<option value=1>1
<option value=3>3
<option value=5>5
<option value=10>10
</select>

-kaht

banghead.gif
 
DOH! Good call. Since I have you on the line... What can I add to *this* code to allow for null values? If the user accidentally clicks into one of these fields, the alert pops up because the onbBlur event says there must be a value. I have to allow nulls because I have another script that averages these fields by the not-null values. (if that makes sense.

function valuevalidation(entered, min, max, alertbox, datatype){

with (entered)
{
checkvalue=parseFloat(value);
if (datatype)
{smalldatatype=datatype.toLowerCase();
if (smalldatatype.charAt(0)=="i") {checkvalue=parseInt(value)};
}
if ((parseFloat(min)==min && checkvalue<min) || (parseFloat(max)==max && checkvalue>max) || value!=checkvalue)
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}

//This is the onBlur event:
/* <input name="P" type="text" onBlur="valuevalidation(this, 1, 5,'Value MUST be an Integer in the range: 1-5');" onChange="add();" size="2">
*/
 
do not use onblur use onchange

onblur is very annoying

all form validation should be done in the form tag using onsubmit

jAy
 
This code will allow a null value, and is also nice cause you get to give the users a small message. Onchange is also the preferred event handler for pulldowns.
Code:
<select name=blahSelect>
<option value=''>Please select a value
<option value=.5>0.5
<option value=1>1
<option value=3>3
<option value=5>5
<option value=10>10
</select>

-kaht

banghead.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top