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

Jan 28th, 2010, 05: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?


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";
c[j].className ="changeToRed";

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


* 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">

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

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