By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,689 Members | 1,915 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,689 IT Pros & Developers. It's quick & easy.

Validataion???

P: n/a
I have two fields (username & department) that i need the user to
update so i require a little bit of JavaScript to validate to code:

Here is what i have in my HEAD tag...

<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function ValidateForm(form)
{

if (IsEmpty(form.username))
{
alert("Please enter your user name.")
form.username.focus();
return false;
}

if (IsEmpty(form.department))
{
alert("Please enter the department you work for.")
form.department.focus();
return false;
}
else return true;
}

</script>
</HEAD>

and here is what i have in my body tag...

<BODY>
<form name="submitform" action="--WEBBOT-SELF--" method="POST"
onSubmit="return ValidateForm(this)" >
<!--webbot bot="SaveResults" U-File="../_private/form_results.csv"
S-Format="TEXT/CSV" S-Label-Fields="TRUE" startspan --><input
TYPE="hidden" NAME="VTI-GROUP" VALUE="0"><!--webbot bot="SaveResults"
i-checksum="43374" endspan -->

Please enter your User Name and your Department.<br><br>
<table border=0>
<tr>
<td align=center>User Name</td>
<td</td>
<td align=center>Department</td>
</tr>
<tr>
<td align=center><input type=text name=name size=10></td>
<td></td>
<td align=center><input type=text name=dept size=10></td>
</tr>
<tr>
<td colspan=3 align=center><input type=submit value="Submit!"></td>
</tr>
</table>
</form>
</BODY>

When i add a user name and click the submit button i get a Confirmation
page???
What i really want is a pop up box to appear if the fields are blank
when the user clicks the submit button, this should advise the user
that the blank fields need to be filled out.
Can anyone help?
Regards
Betty

Sep 25 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Betty wrote:

Hi,
if (IsEmpty(form.username))
I guess that the IsEmpty function looks like

function IsEmpty(field) {
return /^\s*$/.test(field.value);
}
<td align=center><input type=text name=name size=10></td>
<td align=center><input type=text name=dept size=10></td>
Name attributes in the form are "name" and "dept", but in the script
they've been turned to "username" and "department". Just correct this,
and it should work.

BTW Betty this code is poorly presented, there's no indentation and
noisy tags, which is harmful to the reading and (in the long run)
maintenance. A bit of cleaning should be put on the "to do" list ;)
What i really want is a pop up box to appear if the fields are blank
when the user clicks the submit button, this should advise the user
that the blank fields need to be filled out.
---
<script type="text/javascript">
function validateForm(form){
var notes=[], fields=["username", "department"];
for(var ii=0, k=0, v; ii<fields.length; ii++)
if(isEmpty(form[v=fields[ii]].value))
notes.push((++k)+" - Please fill in the "+v+" field.");
return !(notes.length>0 && !alert(notes.join("\n")));

function isEmpty(s){
return /^\s*$/.test(s);
}
}
</script>

<form action="foo" onsubmit="return validateForm(this)">
<table>
<tbody>
<tr>
<td>Username</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Department</td>
<td><input type="text" name="department"></td>
</tr>
</tbody>
</table>
<input type="submit">
</form>
---
Cheers.
Sep 25 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.