PDA

View Full Version : If text value is in an array???


gillweb
04-26-2006, 01:24 PM
I'm look to take the value of a textbox in a form, and see if certain keywords or words are in the value. it'sfor an intranet and it is to look to see if the value of the textbox contains a certain name. If it does it alerts and doesn't submit the form. If it doens't then it goes through normally. I have this simple yet messy code so far but it's not working:

function FastPay( theform )
{
var nochecks = new Array()
nochecks[0] = "MY NAME"
nochecks[1] = "YOUR NAME"
nochecks[2] = "HIS NAME"

var bMissingFields = false;
var strFields = "";

if( theform.name.value == '' ){
bMissingFields = true;
strFields += " Text Field Is empty!\n";
}
else if ( theform.name.value == nochecks )
{
bMissingFields = true;
strFields += "Your field contains text in the array!\n" + theform.name.value;
}

if( bMissingFields ) {
alert( "You must provide the following field(s) before continuing:\n" + strFields );
return false;
}

return true;
}

So I need it to seach through the "nochecks" array and if the value of the textbox is "MY NAME" then it would alert and not submit the form. I assume i'm not calling the fuinctions correctly here: theform.name.value == nochecks

help please :-)

RysChwith
04-26-2006, 02:16 PM
Welcome to the for loop.for( var i = 0; i < nochecks.length; i++ ) {
if( theform.name.value == nochecks[ i ] ) {
bMissingFields = true;
strFields += "Your field contains text in the array.\n" + theform.name.value;
}
}A for loop uses an iterator (i in this case) and continues executing while a condition remains true (i < nochecks.length). With each iteration, a particular operation is performed on the iterator (i++).

Rys

gillweb
04-26-2006, 02:29 PM
looks like that works RysChwith...thanks for the help!