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 3 of 3
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    45
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Require/Restrict Text Input

    -- Figured it out --

    Code:
    if (f.registration.value.length != 8)
    I've created a simple validation script that requires each field be completed. However, the registration key HAS to be 8 digits, no more/no less. Is there a way to easily modify my existing code to include such requirements?

    I've search around and found some solutions I've tried merging into my existing code but not luck. Just becomes a mess.

    Code:
    <script type="text/javascript"> 
    
    function validateForm(f)
    {
    // If they entered an email address, then edit it before submitting the form
    
    // Require Registration
     if (f.registration.value == '')
     {
      alert('Please enter your registration number.')
      f.registration.focus()
      return false;
     } 
    
    // Require Email Address
     if (f.email.value == '')
     {
      alert('Please enter your email address.')
      f.email.focus()
      return false;
     } 
     
     // Require First Name
     if (f.firstname.value == '')
     {
      alert('Please enter your first name.')
      f.firstname.focus()
      return false;
     } 
     
     // Require Last Name
     if (f.lastname.value == '')
     {
      alert('Please enter your last name.')
      f.lastname.focus()
      return false;
     } 
    
    }
    
    // -->
    </script>
    Code:
    <form action="" method="post" enctype="multipart/form-data" onsubmit="return validateForm(this)">
        
    Registration Key:<br />
    <input type="text" id="registration" class="text_field" name="registration" />
                 
    </form>
    ---- REMOVED EXTRA CODE ----
    Last edited by michaelbt2; 04-17-2013 at 09:24 PM. Reason: Removed extra code.

  • #2
    New Coder
    Join Date
    May 2009
    Posts
    45
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I've tried appending the length restriction to the above code and that does not work. It looks like it should work...

    "If registration is not given a value and that value does not equal the length of 8 then alert."

    Code:
    // Require Registration
     if (f.registration.value == '' && f.registration.length !== 8)
     {
      alert('Please enter your registration number.')
      f.registration.focus()
      return false;
     }

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    "If registration is not given a value and that value does not equal the length of 8 then alert."
    || (or) not && (and).


    But how can eight alpha characters (abcdefgh) meet your requirement for a number?



    Code:
    var val = f.registration.value;
    if (!/^\d{8}$/.test(val)) {
    alert("Please enter your registration number - must be exactly 8 digits.");
    f.registration.value = "";  // clear the field
    f.registration.focus();  // refocus on it
    return false;
    }

    Are you sure that any 8 digits will suffice - even 00000000?

    You should note that form validation of the pattern if (document.formname.formfield.value == "") - that is blank - is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. A proper name may only contain letters, hyphen, space and apostrophe.
    Numeric values, such as zip codes, phone numbers and dates, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.


    alerts are regarded as obsolete and should be used ony for testing purposes. Use DOM methods to display a message to your user.


    "If only God would give me some clear sign! Like making a large deposit in my name in a Swiss bank." - Woody Allen - US movie actor, comedian, & director (1935 - )
    Last edited by Philip M; 04-17-2013 at 10:08 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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