...

View Full Version : Selecting Certain Text fields for validation



jcdevelopment
09-11-2008, 03:45 PM
Is there a way we can select only a few text fields out of a lot?

For example, i have close to 50 Text fields, i only need ten of them validated.. Is there an easy way to achieve this, like


document.getElementById('name1' , 'name2' , 'name3').value = ""

would that work?

Philip M
09-11-2008, 04:20 PM
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?

In principle the way is to assign classes:-

<input type="text" ...... class="validate-alphanumeric">

jcdevelopment
09-11-2008, 04:36 PM
Well.. ok i have like i said about "50" fields.

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!

Philip M
09-11-2008, 05:09 PM
Well.. ok i have like i said about "50" fields.

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.


Lottery: A tax on people who are bad at math.

jcdevelopment
09-11-2008, 05:19 PM
Sorry, "I" confused my self on that one.. let me explain it the right way

50 text fields in my form..

10 of them must have some kind of content in them..

the other 40 wont matter.. i need to specifically validate those ten so that they are filled out.

So what I need is to pinpoint those fields only, that's why I asked if I could just list them with a getElementById

Philip M
09-11-2008, 07:18 PM
Try this (obviously the alerts are for testing only):-


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

Philip M
09-12-2008, 01:05 PM
Here is an alternative which does not require your fields to have any particular name:-


<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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum