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 7 of 7
  1. #1
    Senior Coder jcdevelopment's Avatar
    Join Date
    Oct 2007
    Location
    Cowboy Nation
    Posts
    2,182
    Thanks
    174
    Thanked 257 Times in 257 Posts

    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?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    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">

  • #3
    Senior Coder jcdevelopment's Avatar
    Join Date
    Oct 2007
    Location
    Cowboy Nation
    Posts
    2,182
    Thanks
    174
    Thanked 257 Times in 257 Posts
    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!

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    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.

  • #5
    Senior Coder jcdevelopment's Avatar
    Join Date
    Oct 2007
    Location
    Cowboy Nation
    Posts
    2,182
    Thanks
    174
    Thanked 257 Times in 257 Posts
    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

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    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

  • Users who have thanked Philip M for this post:

    jcdevelopment (09-11-2008)

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    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>


  •  

    Tags for this Thread

    Posting Permissions

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