...

View Full Version : check for null textfield when submitting a form



LZS
08-22-2002, 07:09 AM
hi..i need to do a check if the user didn't enter anything in the textfield as he submits a form.i was told to do it using javascript
with the pop up message of which textfield is null.what is the code or function to do this?can anyone help?thanks.

joh6nn
08-22-2002, 07:29 AM
do you want to check one form field, or multiple fields? and can we see the code for the form? that would help.

LZS
08-22-2002, 08:03 AM
i need to do multiple checks in multiple mandatory textfields,
previously i did the checks in the perl program after this page,
but i was told it will save some processing time if i could simply check for null values before submission.i didn't know how to do it in JS.how you can guide me.

#####this is my html page which i print in perl########
print "Content-type: text/html\n\n";

print"<form name=\"form1\" method=\"post\" action=\"/cgi-bin/main.pl\">\n";
print"<div align=\"right\">Request Date : $changedateformat\n";
print"<div align=\"center\"> \n";

print"<p align=\"left\"><u>Organisation Details</u></p>\n";
print"<table width=\"100%\" border=\"1\" height=\"30\">\n";
print"<tr>\n";
print"<td width=\"40%\" height=\"15\"><font color=\"yellow\">*</font>Organisation:\n";
print"<select name=\"org\">\n";
foreach $value (@list_of_orgcodes)
{ print "<option value=\"$value\">$value\n"; }
print"</select>";
print"</td>";

print"<td width=\"80%\" height=\"15\"><font color=\"yellow\">*</font>Site:\n";
# print"<select name=\"site\">\n";
print "<select name=\"schools\">\n";
foreach $value (@list_of_schools)
{ print "<option value=\"$value\">$value\n"; }
print"</select>\n";
print"</td>\n";
print"</tr>\n";

print"</table>\n";
print"<p>&nbsp;</p>\n";
print"<p align=\"left\"><u>User Details </u></p>\n";
print"<table width=\"100%\" border=\"1\" align=\"center\">\n";
print"<tr> \n";
print"<td width=\"16%\"><font color=\"yellow\">*</font>First Name:</td>\n";
print"<td width=\"34%\"> \n";
print"<input type=\"text\" name=\"fn\" maxlength=\"50\">\n";
print"</td>\n";
print"<td width=\"11%\"><font color=\"yellow\">*</font>Last Name:</td>\n";
print"<td width=\"39%\"> \n";
print"<input type=\"text\" name=\"ln\" maxlength=\"50\">\n";
print"</td>\n";
print"</tr>\n";
print"<tr> \n";
print"<td width=\"16%\">Designation:</td>\n";
print"<td width=\"34%\"> \n";
print"<input type=\"text\" name=\"design\" value=\"Teacher\" maxlength=\"70\">\n";
print"</td>\n";
print"<td width=\"11%\">Eff Date:<br><font size=1>(YYYY-MM-DD)</font></td>\n";
print"<td width=\"39%\">\n";
print"<input type=\"text\" name=\"effDate\" value=\"$Eff_Date\">\n";#sysdate +1
print"</td>\n";
print"</tr>\n";
print"<tr>\n";
print"<td width=\"16%\"><font color=\"yellow\">*</font>NRIC:</td>\n";
print"<td width=\"34%\">\n";
print"<input type=\"text\" name=\"Nric\" maxlength=\"9\">\n";
print"</td>\n";
print"<td width=\"16%\">Chinese Name:</td>\n";
print"<td width=\"34%\">\n";
print" <select name=\"select\">\n";
print"<option value=\"Y\">Yes</option>\n";
print" <option value=\"N\">No</option>\n";
print" </select>\n";
print"</td>\n";

print"<input type=\"hidden\" name=\"reqDate\" value=\"$Req_Date\">\n";#change to sysdate
print"</tr>\n";
print"</table>\n";
print"<p align=\"right\">\n";
print"<input type=\"submit\" name=\"Submit\" value=\"Submit\">\n";
print"<input type=\"reset\" name=\"Submit2\" value=\"Reset\">\n";
print "<P align=\"left\"><a href=\"http://localhost:8080/checkstatus/AdminRequestType.htm\">CLICK HERE TO RETURN TO HOME PAGE</a>";
print"</p>\n";
print"</div>\n";
print"</div>\n";
print"</form>\n";

adios
08-22-2002, 08:06 PM
See if this helps:

<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">

var msg = new Object();
msg['whatever1req'] = 'Do something with whatever1.';
msg['whatever3req'] = 'Do something with whatever3.';

function checkEmpty(f) {
var el , e = 0;
while (el = f.elements[e++]) {
if (el.name && el.name.toLowerCase().indexOf('req') != -1 &&
el.type == 'text' && !el .value) {
alert(msg[el.name]);
el.focus();
return false;
}
}
return true;
}

</script>
</head>
<body>
<form onsubmit="return checkEmpty(this)">
whatever1<input type="text" name="whatever1req"><br>
whatever2<input type="text" name="whatever2"><br>
whatever3<input type="text" name="whatever3req"><br>
<input type="submit">
</form>
</body>
</html>

Assuming your required fields are flagged as illustrated. Key the prompt (msg) to the field name.

LZS
08-23-2002, 03:02 AM
hi..thanks for your help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum