...

View Full Version : Newbie Question: How to make form text field check for list of values?



Neophyte
08-05-2002, 07:33 AM
I'm a newbie to JS.

What i want to do is check the value of an input text field (onChange) and compare it with a list of values already grabbed from a DB (these will be text names). If it the text is not any of those values, i want a popup box to appear. I know how to do the popup box text, but am stumped by the rest of it. Any ideas? thanks.

glenngv
08-05-2002, 08:51 AM
i think it's better to use onblur event instead of onchange, because you can't force user to change the value of the textbox if onchange is used.

var arrNames = ("text1","text2","text3"...); //values from db

function checkName(objTxt){
var found=false;
for (var i=0;i<arrNames.length;i++){
if (objTxt.value==arrNames[i]){
found=true;
break;
}
}
if (!found){
alert("'"+objTxt.value+"' not found.");
objTxt.focus();
return false;
}
return true;
}

then in your html:
<input name="text" value="somevalue" onblur="return checkName(this)">

Neophyte
08-06-2002, 01:11 AM
hey, thanks! i'm at work so i can't try it right now but i will when i get home. much appreciated!:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum