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 5 of 5

Thread: validation help

  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,466
    Thanks
    15
    Thanked 0 Times in 0 Posts

    validation help

    I have a bunch of chexboxes and textboxes

    Code:
    <input name="delete" value="626235" type="checkbox"><br><input name="deletedreason626235" type="text">
    
    <input name="delete" value="626236" type="checkbox"><br><input name="deletedreason626236" type="text">
    
    <input name="delete" value="626237" type="checkbox"><br><input name="deletedreason626237" type="text">
    
    <input name="delete" value="626238" type="checkbox"><br><input name="deletedreason626238" type="text">
    I need to do validation that if the checkbox was checked then the corresponding textbox (with the same number must also be checked)

    can someone help men with this?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,316
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    Quote Originally Posted by esthera View Post
    I need to do validation that if the checkbox was checked then the corresponding textbox (with the same number must also be checked)
    What does that mean? Completed? Enabled? Disabled?


    Quizmaster: In 1985 the first black player to win the Wimbledon men's singles title was Arthur who?
    Contestant: Askey

  • #3
    Senior Coder
    Join Date
    May 2004
    Posts
    1,466
    Thanks
    15
    Thanked 0 Times in 0 Posts
    if the checkbox is checked - then the textbox with the same number must not be blank

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,316
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    Code:
    626235 <input name="delete" id = "d1" value="626235" type="checkbox" onchange = "confirmreason(this)"><input id="deletedreason626235" type="text"><br>
    
    626236 <input name="delete" id = "d2" value="626236" type="checkbox" onchange = "confirmreason(this)"><input id = "deletedreason626236" type="text"><br>
    
    626237 <input name="delete" id = "d3" value="626237" type="checkbox" onchange = "confirmreason(this)"><input id = "deletedreason626237" type="text"><br>
    
    626328 <input name="delete" id= "d4" value="626238" type="checkbox"  onchange = "confirmreason(this)"><input id = "deletedreason626238" type="text"><br>
    
    <script type = "text/javascript">
    
    function confirmreason(which) {
    var n = "deletedreason" + which.value;
    var x = document.getElementById(n).value;
    x = x.replace(/^\s+|\s+$/g,"");  // strip leading and trailing spaces;
    if (x.length < 6) {  // minimum six characters for reason
    alert ("You must specify the reason \(minimum 6 characters\) before you can check the checkbox");
    var boxid = which.id;
    document.getElementById(boxid).checked = false;
    return false;
    }
    
    }
    
    </script>

    But why have checkboxes at all? Or if the textbox has been filled (not blank), then regard the corresponding checkbox as checked. On your page layout the user will naturally check the checkbox before entering the reason, so the textbox will always be blank and he is bound to receive an error message.




    The older I grow the more I distrust the familiar doctrine that age brings wisdom. - H.L. Mencken 1880-1956, American Editor, Author, Critic, Humorist
    Last edited by Philip M; 03-17-2009 at 10:08 AM.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,316
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    The following moves the focus to the related textbox if the checkbox is checked, and requires an entry of six characters minimum before the user can proceed. The text entry can be deleted again if the checkbox is first unchecked.

    Code:
    626235 <input name="delete" id = "d626235" value="626235" type="checkbox" onclick = "confirmreason(this)"><input id="deletedreason626235" type="text" onblur = "checkreason(this)"><br>
    
    626236 <input name="delete" id = "d626236" value="626236" type="checkbox" onclick = "confirmreason(this)"><input id = "deletedreason626236" type="text" onblur = "checkreason(this)"><br>
    
    626237 <input name="delete" id = "d626237" value="626237" type="checkbox" onclick = "confirmreason(this)"><input id = "deletedreason626237" type="text" onblur = "checkreason(this)"><br>
    
    626328 <input name="delete" id= "d626238" value="626238" type="checkbox"  onclick = "confirmreason(this)"><input id = "deletedreason626238" type="text" onblur = "checkreason(this)"><br>
    
    <script type = "text/javascript">
    
    function confirmreason(which) {
    
    var a = which.value;
    var b = which.id;
    var n = "deletedreason" + a;
    var x = document.getElementById(n).value;
    x = x.replace(/^\s+|\s+$/g,"");  // strip leading and trailing spaces;
    if (document.getElementById(b).checked == false) {
    document.getElementById(n).value = "";
    return false;
    }
    if (x.length <6) {
    document.getElementById(n).focus();
    }
    }
    
    function checkreason(which) {
    
    var x = which.value;
    b = which.id;
    b = b.replace(/eletedreason/,"")
    
    if (document.getElementById(b).checked == false) {
    if (x.length > 0) {
    alert ("You must check the checkbox before you enter a reason" );
    which.value = "";
    return false;
    }
    }
    
    if (document.getElementById(b).checked) {
    x = x.replace(/^\s+|\s+$/g,"");  // strip leading and trailing spaces;
    if (x.length < 6) {
    alert ("You must enter a reason for deleting this item \(6 characters minimum\)");
    which.focus();
    return false;
    }
    }
    
    }
    
    </script>
    Last edited by Philip M; 03-17-2009 at 12:45 PM. Reason: Improved and bug corrected


  •  

    Posting Permissions

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