deadlock Message Board
[ May Full List | Post A Reply ]

Re: form validation
From Zac on 27 March '05
replying to form validation posted by tom

>Hello,
>
>I someone can actually help me with this one, I will be so grateful... I am looking for a script (preferably Javascript because I do not have PERl installed on my server, but I was wondering someone has any cod for a form validator. Here's the catch: I need to it to be able give different alerts for different fields not being filled in. Not just one alert that says one of the required feilds is not filled in. One of the feilds is also going to be an email so the script needs to be able to validate it. Also one box of the form will be a CC number so just in case someone doesn't put enough digits the script will also give an alert.
>
>I know this may seem tricky or like alot but any help, advise or tips would be greatly appreciated!
>
>Thanks in advance!!!!!!!!!

Hi, This is a script I found for you. I found it on <a
href="http://www.dynamicdrive.com">
dynamic drive</a> may i
suggest next time searching various html/javascript sites to see
if they have the answer.

the script

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

/***********************************************
* Required field(s) validation v1.10- By NavSurf
* Visit Nav Surf at http://navsurf.com
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function formCheck(formobj){
// Enter name of mandatory fields
var fieldRequired = Array("FirstName", "LastName");
// Enter field description to appear in the dialog box
var fieldDescription = Array("First Name", "Last Name");
// 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>

<!--SAMPLE FORM -------------------------------->
<form name="formcheck" onsubmit="return formCheck(this);">
First Name: <input type=text name="FirstName" size="25"><br>
Last Name: <input type=text name="LastName" size="25"><br>
<input type=submit value="Submit Form">
</form>




Your Reply

*Your name:
*Message subject:
Home page title:
Home page URL:
Email:
(if you'd like to receive automatic replies by email)