Validataion???

B

Betty

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
 
E

Elegie

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top