Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-11-2008, 03:45 PM   PM User | #1
jcdevelopment
Senior Coder

 
jcdevelopment's Avatar
 
Join Date: Oct 2007
Location: Cowboy Nation
Posts: 2,171
Thanks: 173
Thanked 257 Times in 257 Posts
jcdevelopment will become famous soon enoughjcdevelopment will become famous soon enough
Selecting Certain Text fields for validation

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

Code:
document.getElementById('name1' , 'name2' , 'name3').value = ""
would that work?
jcdevelopment is offline   Reply With Quote
Old 09-11-2008, 04:20 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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">
Philip M is offline   Reply With Quote
Old 09-11-2008, 04:36 PM   PM User | #3
jcdevelopment
Senior Coder

 
jcdevelopment's Avatar
 
Join Date: Oct 2007
Location: Cowboy Nation
Posts: 2,171
Thanks: 173
Thanked 257 Times in 257 Posts
jcdevelopment will become famous soon enoughjcdevelopment will become famous soon enough
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!
jcdevelopment is offline   Reply With Quote
Old 09-11-2008, 05:09 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by jcdevelopment View Post
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.
Philip M is offline   Reply With Quote
Old 09-11-2008, 05:19 PM   PM User | #5
jcdevelopment
Senior Coder

 
jcdevelopment's Avatar
 
Join Date: Oct 2007
Location: Cowboy Nation
Posts: 2,171
Thanks: 173
Thanked 257 Times in 257 Posts
jcdevelopment will become famous soon enoughjcdevelopment will become famous soon enough
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
jcdevelopment is offline   Reply With Quote
Old 09-11-2008, 07:18 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
jcdevelopment (09-11-2008)
Old 09-12-2008, 01:05 PM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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>
Philip M is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript, validation

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:28 AM.


Advertisement
Log in to turn off these ads.