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
    Jul 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Validate form field only if it is filled in.

    In addition, whatever they input should be only numbers and contain at least 3 digits. Can one of you pros help me out?

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this:

    Code:
    <script type="text/javascript">
      function validate(input){
        if(input.value != '' && !input.value.match(/^\d{3,}$/)){
           alert('You must enter at least three numerics\nand no alpha characters!!');
           input.value = '';
           input.focus();
           return false;
        }    
      }
    </script>
    </head>
    
    <body>
    <form onsubmit="return validate(this.test)">
    <input type="text" name="test">
    <input type="submit" value="Submit">
    </form>
    .....Willy

    Edit: Or on the input....
    Code:
    <form>
    <input type="text" name="test"
    onblur="if(this.value!='' && !this.value.match(/^\d{3,}$/)){
    alert('You must enter at least three numerics\nand no alpha characters!!');
    this.value='';this.focus()}">
    </form>
    Last edited by Willy Duitt; 07-16-2004 at 06:53 PM.

  • #3
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey Willy, thanks for the response.

    I have is 3 search fields and at least one of them must be filled out before the user submits. The first part of the script accomplishes this. However, if the order number happens to be filled out, than check it to see if it has only numbers and at least 3 digits.

    I figured I would take what you gave me and everything would be peachy. It seems to completely break. Here is what I have:

    <script>
    function ValidateForm(form1){
    if ((form1.ord_no.value == "") && (form1.ord_dt.value == "") && (form1.job_no.value == "")){
    alert('Input in one field is required.');
    return false;}
    {
    if(form1.ord_no.value != '' && !form1.ord_no.value.match(/^\d{3,}$/)){
    alert('The PO number must contain at least 3 digits for a partial search. Alpha characters can not be submitted.');
    return false;
    }

    return true;
    }
    </script>

    I also tried putting it in the input tag like this.

    <input type="test" name="ord_no"onblur="if(this.value!='' && !this.value.match(/^\d{3,}$/)){
    alert('You must enter at least three numerics\nand no alpha characters!!');
    this.value='';this.focus()}">

    It doesnt seem to effect it at all. Am I missing something?

  • #4
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Grrr ... perhaps I should save my changes before I post. Anyhow your second suggestion worked. Thanks Willy, I appreciate the time you took to help me out.

  • #5
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Your welcome

    I only started with regular expressions a few weeks ago and each one I write brings me closer to understanding what I am doing...

    BTW: Your post had a typo....
    <input type="test" .....
    You may want to check your code....

    .....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
    •