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 8 of 8
  1. #1
    New Coder
    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Validate At Least One Field is Filled

    Hi,

    I'm trying to visualise how should I validate a form that only requires that only one of the fields is filled.

    I've the validation code to validate mandatory fields. But how should I code this if there are maybe 5 fields (different types) and I want the user to enter the search keywords in any of the fields?

    I believe that's how the advanced search in this forum is done.

    Here's what I have:
    Code:
    function validateAdvSearch()
    {
    var f = document.forms[0];
    
    if (validateText(f.Name, 'Name') == false || validateCombobox(f.Country, 'Country') == false)
    return false;
    
    else if (validateDate(f.Date, 'Date') == false)
    return false;
    else
    return true;
    }
    Hmm... it doesn't seem the correct way to code it, anyone can point me in the right direction?

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I'm trying to visualise how should I validate a form that only requires that only one of the fields is filled.
    Circle through elements
    PHP Code:
    function validateAdvSearch(f){
    var 
    q=0;
    var 
    inp document.getElementsByTagName('form')[0].getElementsByTagName('input');
    for(var 
    i=0;i<inp.lenght;i++){
    if(
    inp.getAttribute('type')=='text'){
    if(
    inp[i].value.length>0){
    q++
    }
    }
    }
    if(
    q==0){
    alert('Please fill at least one field!');
    return 
    false;
    }
    }
    ...
    ...
    <
    form onsubmit="return validateAdvSearch(this)"
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Kor's solution is pretty nifty.

    I often have the problem that at least one of a specific subset of fields must be entered. I create an array of those values and then search the array.

    This technique is also pretty flexible. If your list is different, and as fields are added or removed over time, only the "buildAtLeast1Array()" needs to be touched.

    Also, by passing in the form object as a parameter to the function, rather than reference the form directly in the function, the function(s) are not "coupled" to a specific form / web page - thus the code is more "cut and pasteable", i.e. reusable.

    PHP Code:
    function buildAtLeast1Array(myForm) {
      var 
    theList = new Array();
      
    theList.push(myForm.field1.value);  // repeat this line as necessary,
                                          // Taylored to type of input tag

      
    return theList;
    }

    // by passing in the entryList, this function is reusable for 
    // any subset (array) of entry fields.
    function anythingEnteredIn(entryList) {
      var 
    isEntered true;

      
    nextEntry:
      for (
    i=0i<entryList.lengthi++) {
         if (
    entryList[i] == "") {
            
    isEntered false;
            break 
    nextEntry;  // stop looping now
         
    }
      }

      return 
    isEntered;
    }

    function 
    validate(myForm) {
      var 
    isValid true;
      var 
    myEntryFields buildAtLeast1Array(myForm);

      if (!
    anythingEnteredIn (myEntryFields)) {
         
    isValid false;
         
    alert("you must make at least one entry");
      }

    // enter other form validations, setting 'isValid' to false as necessary

      
    return isValid;
    }

    -- 
    meanwhileback on your form tag...
    <
    form name='myForm' method='post' onSubmit='return validate(this);'
    Last edited by RadarBob; 03-09-2005 at 12:33 AM.

  • #4
    New Coder
    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you Kor, Bob!

    I'll test these 2 out

  • #5
    New Coder
    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    I tested both functions but I can't get them to work.
    I used Kor's function, there's an error in this line:
    Code:
    if(inp.getAttribute('type')=='text'){
    I'm assuming that it can continue looping if the the form contains only textboxes? So, I should add in the following:
    Code:
    else if(inp.getAttribute('type') == 'select') { blah blah.. }
    As for Bob's the error points that field1.value is null or blank:
    Code:
    theList.push(myForm.field1.value);
    I tried to change it to my field name but the same error occur. I can't make head or tails of what it means.

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, hard to say what's up.

    Make sure that in "myForm.field1.value" that 'field1' is the the name attribute; spelled correctly. Capitalization counts.

    Next ... "is null or blank". I'd say blank is an expected condition - sometimes we don't enter anything into a field. If it's null, that could indicate that you do not "have a hold" of the field - like maybe it's spelled wrong.

    Next try alert() inside buildatLeast1Array() to see what the values are.

  • #7
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    Here's some more code to try.
    Code:
    <script type="text/javascript">
    function validateAdvSearch() {
    var f = document.forms[0];
    var num = f.elements.length;
    var j = 0;
    for (var i=0; i<num; i++) {
     if (f.elements[i].type == 'checkbox') { if (f.elements[i].checked)  {j = j+1; } }
     if (f.elements[i].type == 'text') { if (f.elements[i].value) { j = j+1; } }
     else { continue; }
     }
    if (j == 0) { alert('At Least One Field Must Contain a value'); }
    }
    </script>
    /Bob

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    my fault, should've been

    if(inp[i].getAttribute('type')=='text'){
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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