Jump to content

need help with validation


dezkit

Recommended Posts

Every time i press submit when i have phone filled out, it says i didnt fill itout.

 

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

function formCheck(formobj){
// Enter name of mandatory fields
var fieldRequired = Array("FullName", "Phone");
// Enter field description to appear in the dialog box
var fieldDescription = Array("Full Name", "Phone");
// 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 action="/site/index.php?page=thanks" method="post" onsubmit="return formCheck(this);">
<label style="float:left;width:20px;"> </label><label style="float:left;width:140px;">Full Name<font color="red">*</font>:</label><input type="text" name="FullName" id="Phone" value="" maxlength="" style="width:180px;"><div style="clear:left;height:0px;"> </div>

<label style="float:left;width:20px;"> </label><label style="float:left;width:140px;">Age:</label><input type="text" name="Age" id="Phone" value="" maxlength="" style="width:180px;"><div style="clear:left;height:0px;"> </div>

<label style="float:left;width:20px;"> </label><label style="float:left;width:140px;">Phone<font color="red">*</font>:</label><input type="text" name="Phone" id="Phone" value="" maxlength="" style="width:180px;"><div style="clear:left;height:0px;"> </div>

<label style="float:left;width:20px;"> </label><label style="float:left;width:140px;">E-Mail:</label><input type="text" name="Email" id="Phone" value="" maxlength="" style="width:180px;"><div style="clear:left;height:0px;"> </div>

<label style="float:left;width:20px;"> </label><label style="float:left;width:140px;">AIM Screen Name:</label><input type="text" name="AIM" id="Phone" value="" maxlength="" style="width:180px;"><div style="clear:left;height:0px;"> </div>

<label style="float:left;width:20px;"> </label><label style="float:left;width:140px;">Myspace URL:</label><input type="text" name="Myspace" id="Phone" value="" maxlength="" style="width:180px;"><div style="clear:left;height:0px;"> </div>

<br>
<center>
<input type='submit' value='Submit'>
<input type="reset" value="Reset">
</center>
</body>

 

 

i know all the id's are set to phone but im too lazy to remove it.

Link to comment
https://forums.phpfreaks.com/topic/93040-need-help-with-validation/
Share on other sites

Having all set to phone was one of your main problems...

 

 

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

function formCheck(formobj){
// Enter name of mandatory fields
var fieldRequired = Array("FullName", "Phone");
// Enter field description to appear in the dialog box
var fieldDescription = Array("Full Name", "Phone");
// 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":
			if (obj.value == "" || obj.value == null){
				alertMsg += " - " + fieldDescription[i] + "\n";
			}
			break;
		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){
	alert(alertMsg);
	return false;
}
document.myform.submit();
}
// -->
</script>



<form action="/site/index.php?page=thanks" method="POST" name="myform" onsubmit="return formCheck(this);">
<label style="float:left;width:20px;"> </label><label style="float:left;width:140px;">Full Name<font color="red">*</font>:</label><input type="text" name="FullName" id="FullName" value="" maxlength="" style="width:180px;"><div style="clear:left;height:0px;"> </div>

<label style="float:left;width:20px;"> </label><label style="float:left;width:140px;">Age:</label><input type="text" name="Age" id="Age" value="" maxlength="" style="width:180px;"><div style="clear:left;height:0px;"> </div>

<label style="float:left;width:20px;"> </label><label style="float:left;width:140px;">Phone<font color="red">*</font>:</label><input type="text" name="Phone" id="Phone" value="" maxlength="" style="width:180px;"><div style="clear:left;height:0px;"> </div>

<label style="float:left;width:20px;"> </label><label style="float:left;width:140px;">E-Mail:</label><input type="text" name="Email" id="Email" value="" maxlength="" style="width:180px;"><div style="clear:left;height:0px;"> </div>

<label style="float:left;width:20px;"> </label><label style="float:left;width:140px;">AIM Screen Name:</label><input type="text" name="AIM" id="AIM" value="" maxlength="" style="width:180px;"><div style="clear:left;height:0px;"> </div>

<label style="float:left;width:20px;"> </label><label style="float:left;width:140px;">Myspace URL:</label><input type="text" name="Myspace" id="Myspace" value="" maxlength="" style="width:180px;"><div style="clear:left;height:0px;"> </div>

<br>
<center>
<input type='submit' value='Submit'>
<input type="reset" value="Reset">
</center>
</body>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.