Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts

    excluding fields from validation

    I'm using a very basic generic form validation
    Code:
    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?

  • #2
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Something like this?

    Code:
    <!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
    Last edited by martin_narg; 08-16-2005 at 02:07 PM. Reason: Updated with individual element id's
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #3
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    PHP Code:
    <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
    PHP Code:
    function validateForm(oFrm)
    {
        for (var 
    i=0aValFlds=oFrm['valid_field_names'].value.split(","), aElms=oFrm.elementsi<aElms.lengthi++)
        {
            for (var 
    j=0j<aValFlds.lengthj++)
            {
                (
    aElms.name == aValFlds[j]) ? performValidation(): continue;
            }
        }

    *keep it simple (TM)

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Martin, IDs have to be unique. Although that's a good method, you could use classes instead.
    *keep it simple (TM)

  • #5
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,511
    Thanks
    3
    Thanked 504 Times in 491 Posts
    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

  • #6
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    smart thinking both of you
    Thanks!

  • #7
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by vwphillips
    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.
    *keep it simple (TM)

  • #8
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    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
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #9
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    All was working fine... until I opened IE - I dont get any errors but it doesnt work... any ideas why?

    Code:
    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;
    }

  • #10
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Code:
    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
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #11
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    ..I knew that... I'm a numpty


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •