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
  1. #1
    New to the CF scene
    Join Date
    Dec 2004
    Location
    London
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Text Box Validation

    Anyone know of a bit of JavaScript that will alert if the user has entered a value in more that one text box from a set of three.

    One text box must have a value, but only one.

    So far I have the following to check that they are not all empty:

    function validateForm() {
    if (document.upload.discount_price.value == '' &&
    document.upload.discount_percent.value == '' &&
    document.upload.discount_value.value == '') {
    alert ('Please enter a value for either the discount price, percentage, or value.');
    return false;
    }
    else if(

    ...


    Any suggestions would be appreciated.

    Thanks.

  • #2
    New to the CF scene
    Join Date
    Dec 2004
    Location
    London
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ...unless anyone can think of a better version than this:

    function validateForm() {
    if (document.upload.discount_price.value == '' &&
    document.upload.discount_percent.value == '' &&
    document.upload.discount_value.value == '') {
    alert ('Please enter either the discount price, percentage, or value.');
    return false;
    }
    else if (document.upload.discount_price.value != '' &&
    document.upload.discount_percent.value != '' &&
    document.upload.discount_value.value != '') {
    alert ('Please enter only one type of discount.');
    return false;
    }
    else if (document.upload.discount_price.value != '' &&
    document.upload.discount_percent.value != '') {
    alert ('Please enter only one type of discount.');
    return false;
    }
    else if (document.upload.discount_percent.value != '' &&
    document.upload.discount_value.value != '') {
    alert ('Please enter only one type of discount.');
    return false;
    }
    else if (document.upload.discount_price.value != '' &&
    document.upload.discount_value.value != '') {
    alert ('Please enter only one type of discount.');
    return false;
    }
    }

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How about this:
    Code:
    function validateForm() {
    if (document.upload.discount_price.value == '' &&
    document.upload.discount_percent.value == '' &&
    document.upload.discount_value.value == '') {
    alert ('Please enter a value for either the discount price, percentage, or value.');
    return false;
    }
    else 
     {
      count=0
      if(document.upload.discount_price.value.length > 0)
        {count++}
      if(document.upload.discount_percent.value.length > 0)
        {count++}
      if(document.upload.discount_value.value.length > 0)
        {count++}
      if(count>1)
        {alert ("You may only fill in one.")}
     }
    }

  • #4
    New to the CF scene
    Join Date
    Dec 2004
    Location
    London
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That is perfect... Thanks.

  • #5
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A little late but:

    Code:
    <script type="text/javascript">
     <!--//
      function validate(form){
       var hasValue = [], message = '';
        for(var i=0;i<form.elements.length;i++){
         var input = form.elements[i];
          if(input.className.match(/onlyOne/gi) && input.value.length > 0){
             message += '\n'+input.name.replace('_',' ').toUpperCase();
             hasValue.push(input.name);
          }  
        }
    
        if(hasValue.length == 0){
           alert ('Please enter a value for either the:\ndiscount price, percentage, or value.');
           return false;
        }
    
        if(hasValue.length > 1){
          for(var i=0;i<hasValue.length;i++){
              form[hasValue[i]].value = '';
          }   alert('You have chosen:\n'+message+'\n\nPlease chose only one!!');
              return false;
        }     return true;
      }    
     //-->
    </script>
    </head>
    
    <body>
    <form onsubmit="return validate(this)">
    <input class="onlyOne" name="discount_price"><br>
    <input class="onlyOne" name="discount_percent"><br>
    <input class="onlyOne" name="discount_value"><br>
    <input type="submit" value="Test">
    </form>
    .....Willy


  •  

    Posting Permissions

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