PDA

View Full Version : No empty entries for textbox & listbox validation



blurguava
01-28-2010, 06:13 AM
Hi there, I have this form that validates a few textboxes & a dropdownlist. And when it is not filled in, the border of the textboxes and dropdownlist would turn red, followed by an alert message notifying which field have not been filled in.



Else, the border will revert back to black, and the form will be submitted successfully.

Right now, I would like to prevent the user from entering blanks (like entering a space to cheat the system). How should I tweak the codes to make it work?

Javascript



function check(checkForm)
{
var fields = new Array("Name","Email Address", "Domain Name");

var index = new Array(),k=0;
for(var i=0;i<fields.length;i++)
{
var isFilled = false;
var c = document.getElementsByName(fields[i]);
for(var j = 0; j < c.length; j++)
if(!c[j].value == "")
{
isFilled = true;
c[j].className = "defaultColor";
}
else
{
c[j].className ="changeToRed";
}

if(!isFilled)
{
index[k++] = fields[i];
}
}
if(k>0)
{
joinComma = index.join(', ');
alert('The field(s) corresponding to '+ joinComma + ' is/are not selected.');
return false;
}
}


HTML


* Name:
<input type="text" id="Text27" name="Name" /><br />
<br />

*Email Address:
<input type="text" id="Text28" name="Email Address" />
@
<select id="Select5" name="Domain Name">
<option></option>
<option>hotmail.com</option>
<option>yahoo.com</option>
</select>

<input id="Submit5" type="submit" value="Submit" onclick="return check(checkForm)"/>

Old Pedant
01-28-2010, 08:35 AM
Use the same answer I gave you in the other thread. It should work even for the <select>.