...

View Full Version : Question Re. Form Validator



Aiden
01-04-2004, 11:52 PM
I have this form field checker (code as follows) and it tells people what "required fields" haven't been completed when they submit. You define the required fields in an array (as you'll see).

In this particular form (two fields - text and password) I've defined both fields as required - however if you leave both fields empty and press submit it alerts and says that you only left the text field incomplete - it overlooks the password field.

Can somebody tell me what I'm doing wrong? Cheers!

+++++++++++++++++++++++++++

<script language="JavaScript">
<!--

function formCheck(formobj){
// Enter name of mandatory fields
var fieldRequired = Array("UserName", "PassWord");
// Enter field description to appear in the dialog box
var fieldDescription = Array("Username", "Password");
// dialog message
var alertMsg = "Please complete the following fields:\n";

var l_Msg = alertMsg.length;

for (var i = 0; i < fieldRequired.length; i++){
var obj = formobj.elements[fieldRequired[i]];
if (obj){
switch(obj.type){
case "select-one":
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "select-multiple":
if (obj.selectedIndex == -1){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "text":
case "textarea":
if (obj.value == "" || obj.value == null){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
default:
}
if (obj.type == undefined){
var blnchecked = false;
for (var j = 0; j < obj.length; j++){
if (obj[j].checked){
blnchecked = true;
}
}
if (!blnchecked){
alertMsg += " - " + fieldDescription[i] + "\n";
}
}
}
}

if (alertMsg.length == l_Msg){
return true;
}else{
alert(alertMsg);
return false;
}
}
// -->
</script>


<form name="formcheck" onsubmit="return formCheck(this);">
First Name: <input type="text" name="UserName" size="25"><br>
Last Name: <input type="password" name="PassWord" size="25"><br>
<input type="submit" value="Submit Form">
</form>

mpicklesimer
01-05-2004, 03:10 PM
What I've come across most often in programming is that when doing this type of thing, the scripts will often pitch an error on the first error condition that it comes across. Now bear in mind that I haven't looked through your code thoroughly, but here's a suggestion in case you haven't looked at it. Oh, and when I'm posting these possible fixes, I've been doing mostly VB lately, so I'm a little rusty with the JS equivalents. Please bear with me.

Make a section of your javascript that sets a variable for each required form element to either true or false, like "field.password.Value IsValid = True". Then have another section of code use kind of a For... Each statement to pull together a kind of combined error message containing everything about all of the invalid form fields.

Again, I'm sorry if this is hard to follow, but like I said, I've been doing nothing but Visual Basic for about 6 months now. I hope this helps some.

Willy Duitt
01-05-2004, 03:22 PM
You need to define the input type=password in your function.

Try this:
(changes highlighted in red)

<script language="JavaScript">
<!--

function formCheck(formobj){
// Enter name of mandatory fields
var fieldRequired = Array("UserName", "PassWord");
// Enter field description to appear in the dialog box
var fieldDescription = Array("Username", "Password");
// dialog message
var alertMsg = "Please complete the following fields:\n";

var l_Msg = alertMsg.length;

for (var i = 0; i < fieldRequired.length; i++){
var obj = formobj.elements[fieldRequired[i]];
if (obj){
switch(obj.type){
case "select-one":
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "select-multiple":
if (obj.selectedIndex == -1){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "text":
case "password":
case "textarea":
if (obj.value == "" || obj.value == null){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
default:
}
if (obj.type == undefined){
var blnchecked = false;
for (var j = 0; j < obj.length; j++){
if (obj[j].checked){
blnchecked = true;
}
}
if (!blnchecked){
alertMsg += " - " + fieldDescription[i] + "\n";
}
}
}
}

if (alertMsg.length == l_Msg){
return true;
}else{
alert(alertMsg);
return false;
}
}
// -->
</script>


<form name="formcheck" onsubmit="return formCheck(this);">
First Name: <input type="text" name="UserName" size="25"><br>
Last Name: <input type="password" name="PassWord" size="25"><br>
<input type="submit" value="Submit Form">
</form>

.....Willy

Aiden
01-06-2004, 01:12 AM
worked a charm........ thanx



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum