...

View Full Version : excluding fields from validation



NancyJ
08-16-2005, 11:21 AM
I'm using a very basic generic form validation

function validate(obj) {

for (i=0; i<obj.elements.length; i++) {
if(obj.elements[i].value==""){
alert("Please complete all the fields before submitting the form");
obj.elements[i].focus();
return false;
}
}
obj.submit();
}

But I now want to exclude 2 fields from the validation, how can I acheive this without having to write validation for each individual field?

martin_narg
08-16-2005, 11:36 AM
Something like this?



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
function validate(obj) {
for (i=0; obj.elements[i]; i++) {
if(obj.elements[i].getAttribute("id").indexOf("exclude") > -1) {
continue;
}

if(obj.elements[i].value == ""){
alert("Please complete all the fields before submitting the form");
obj.elements[i].focus();
return false;
}
}
return true;
}
</script>
</head>

<body>
<form name="frm" onsubmit="return validate(this);">
This form validation does not require the first 2 fields:<br>
<input type="text" name="txt1" id="exclude1"><br>
<input type="text" name="txt2" id="exclude2"><br>
<input type="text" name="txt3"><br>
<input type="text" name="txt4"><br>
<input type="submit" value="submit">
</form>
</body>
</html>


Just give any non-essential text field the id exclude.

Hope this helps

m_n

jbot
08-16-2005, 11:38 AM
put the names of the fields you want to validate as a comma separated string into an hidden field. then in your validate function, split the value of that hidden field on the commas, creating a new array in the process. when looping round all the fields check their names against those in the field name array, and if they match then you can validate the field.



<form action="" method="" onsubmit="return validateForm(this)">
<input type="text" name="field1" value="">
<input type="text" name="field2" value="">
<input type="text" name="field3" value="">
<input type="text" name="field4" value="">
<input type="text" name="field5" value="">
<input type="hidden" name="valid_field_names" value="field1,field2,field4">
</form>




function validateForm(oFrm)
{
for (var i=0, aValFlds=oFrm['valid_field_names'].value.split(","), aElms=oFrm.elements; i<aElms.length; i++)
{
for (var j=0; j<aValFlds.length; j++)
{
(aElms.name == aValFlds[j]) ? performValidation(): continue;
}
}
}

jbot
08-16-2005, 11:39 AM
Martin, IDs have to be unique. Although that's a good method, you could use classes instead.

vwphillips
08-16-2005, 11:43 AM
BTW
there is no need to expend a valid attribute for this purpose
a custom attribute can be used ie exclude="Y"

then if (getAttribute('exclude'){

the attribute value cant access in browsers other than IE by there is no need in this type of application

NancyJ
08-16-2005, 11:44 AM
smart thinking both of you ;)
Thanks!

jbot
08-16-2005, 11:48 AM
there is no need to expend a valid attribute for this purpose
a custom attribute can be used ie exclude="Y"

if you do that, then you really need to write a custom DTD. otherwise your markup won't validate. yes, I know that's not necessarily the most important thing in the world, but it's becoming increasinly so in a semantic accessible world. also, it may not be the most future proof and could conflict with XHTML 2 or HTML 5, for instance.

martin_narg
08-16-2005, 02:05 PM
Nice one, agree with all, I had originally done it using incremental id's exclude1, exclude2, etc and just done an indexOf("exclude")

changed it over because i had a beer with lunch and felt groovy. You can still have html elements with same id's - it won't error out, but it's bad practice (and hence I should have done it properly) =)

All fun, pleased there's some spirited discussion.

m_n

NancyJ
08-16-2005, 03:43 PM
All was working fine... until I opened IE - I dont get any errors but it doesnt work... any ideas why?


function validate(obj) {
for (i=0; obj.elements[i]; i++) {
if(obj.elements[i].getAttribute("class") == "exclude") {
continue;
}

if(obj.elements[i].value == ""){
alert("Please complete all the fields before submitting the form");
obj.elements[i].focus();
return false;
}
}
return true;
}

martin_narg
08-16-2005, 07:30 PM
function validate(obj) {
for (i=0; obj.elements[i]; i++) {
if(obj.elements[i].className == "exclude") {
continue;
}

if(obj.elements[i].value == ""){
alert("Please complete all the fields before submitting the form");
obj.elements[i].focus();
return false;
}
}
return true;
}


This should do it

m_n

NancyJ
08-17-2005, 09:19 AM
..I knew that... I'm a numpty ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum