I am not entirely clear - do you wish for validation to be applied to only ten out of the fifty fields on your form - if so is the validation the same for all the fields (e.g. alphanumeric characters only)?
Or do you want to validate all the fields but an empty field is acceptable except in ten instances?
10 are ok to pass up. One is a description of something(not needed) another is a combination to something(not needed)
so basically i need 10 "specific" fields not checked!
Sorry i hope that is clear!
No, it is not clear.
Originally you said "For example, i have close to 50 Text fields, i only need ten of them validated." Now you say "i need 10 "specific" fields not checked!"
Does not checked mean that a blank or empty field is acceptable?
BTW - In English the personal pronoun I is a capital letter.
Try this (obviously the alerts are for testing only):-
Code:
1<input type="text" id = "field1">Required<br>
2<input type="text" id = "field2">Required<br>
3<input type="text" id = "field3"><br>
4<input type="text" id = "field4">Required<br>
5<input type="text" id = "field5"> <br>
6<input type="text" id = "field6"><br><br>
<input type = "button" name = "but1" value = "Validate Form" onclick = "validate()">
<script type = "text/javascript">
var x = new Array(0,1,1,0,1,0,0); // validation not required if 0, required if 1
function validate() {
for (var i = 1; i <= 6; i++) {
if (x[i] == 1) {
y = document.getElementById("field" + i).value;
alert ("Validation is required for Field " + i);
y = y.replace(/^\s+/,""); // strip leading spaces
if (y.length == 0) {
alert ("Field No " + i + " must be completed");
document.getElementById("field" + i).focus();
return false;
}
}
else {
alert ("Validation is not required for Field " + i)
}
}
}
</script>
It is your responsibility to die() if necessary… - PHP Manual
Here is an alternative which does not require your fields to have any particular name:-
Code:
<form name = "myform">
1<input type="text" name = "field1" id = "field1">Required<br>
2<input type="text" name = "field2" id = "field2">Required<br>
3<input type="text" name = "field3" id = "field3"><br>
4<input type="text" name = "field4" id = "field4">Required<br>
5<input type="text" name = "field5" id = "field5"> <br>
6<input type="text" name = "field6" id = "field6"><br><br>
<input type = "button" name = "but1" value = "Validate Form" onclick = "validate()">
</form>
<script type = "text/javascript">
var x = new Array(0,1,1,0,1,0,0); // validation not required if 0, required if 1
function validate() {
for (var i = 1; i <= 6; i++) {
if (x[i] == 1) {
y = document.forms[0].elements[i-1].value; //elements[] base value is 0
alert ("Validation is required for Field " + i);
y = y.replace(/^\s+/,""); // strip leading spaces
if (y.length == 0) {
alert ("Field No " + i + " must be completed");
document.forms[0].elements[i-1].focus();
return false;
}
}
else {
alert ("Validation is not required for Field " + i)
}
}
}
</script>