PDA

View Full Version : required field array



abs0lut
08-26-2008, 11:40 PM
I have an input field

<form method="post" name="frmname">
<input type="text" name="txtcost[]" />
<input type="submit" name="btnsubmit">
</form>
when the user leave the field blank, it will display an alert "Please enter'
could you please help me?

clueful
08-27-2008, 12:27 AM
<form method="post" name="frmname" onsubmit="var result; if((result=!this['txtcost[]'].value.match(/\S/))){alert('Please Enter');}return result;">
<input type="text" name="txtcost[]" />
<input type="submit" name="btnsubmit">
</form>

abs0lut
08-28-2008, 12:59 AM
<form method="post" name="frmname" onsubmit="var result; if((result=!this['txtcost[]'].value.match(/\S/))){alert('Please Enter');}return result;">
<input type="text" name="txtcost[]" />
<input type="submit" name="btnsubmit">
</form>

not working, please help..

mburt
08-28-2008, 02:29 AM
You shouldn't use "[]" in the names of form elements. Just use clear, descriptive names.

clueful
08-28-2008, 02:36 AM
not working, please help..It does work.
I hope you meant the alert to appear when the button is pressed, because alerting on blur isn't clever.

mburt
08-28-2008, 02:44 AM
<form method="post" name="frmname">
<input type="text" name="txtcost[]" value="[Enter a value]" onkeyup="if (this.value == '') this.value = '[Enter a value]';" onfocus="this.value = '';" />
<input type="submit" name="btnsubmit">
</form>

Is that what you want?