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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    checking to determine if multiple fields are blank

    I am trying to validate a form. In one case at least one of 4 fields must have a value. I have an isBlank() function that determines if a field is blank. Now I want to test all 4 and throw up an error if they are all blank. Here is what I have ...


    function isBlank(str)
    {
    if (str != null)
    {
    for (i=0;i<str.length;i++)
    {
    if (str.charCodeAt(i) >= 33)
    return false;
    }
    }
    return true;
    }


    if ((isBlank(txtEvent1.value)) && isBlank(txtEvent2.value)) && isBlank(txtEvent3.value)) && isBlank(txtEvent4.value)))
    sErr += 'You must enter at least one ticketing event.\n'

    I have tried adding & removing parentheses .. I can't figure out the syntax error. Thanks for any help!
    Last edited by emkay717; 04-20-2009 at 01:20 AM. Reason: spelling

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,992
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Try this:-

    Code:
    <script type = "text/javascript">
    
    var count = 0;
    function isBlank(str) {
    str = str.replace(/^\s+|\s+$/g,"");  // strip leading and trailing spaces
    str = str.replace(/[^A-Za-z0-9\s]/g,""); // strip all but alphanumeric and whitespace
    if (str.length < 1) {  // blank field
    count ++;
    }
    if (count == 4) {   // all four fields blank
    count = 0;
    return false;  // i.e. validation failed
    }
    return true;
    }
    
    if ((isBlank(txtEvent1.value)) && (isBlank(txtEvent2.value)) && (isBlank(txtEvent3.value)) && (isBlank(txtEvent4.value)))
    sErr += 'You must enter at least one ticketing event.\n'
    
    </script>
    Last edited by Philip M; 04-20-2009 at 12:17 PM. Reason: Typo


  •  

    Posting Permissions

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