Skip to main content
October 1, 2007
Question

Minimum length in form field

  • October 1, 2007
  • 5 replies
  • 2160 views
I am working on a form where a few fields are required. Two of the required fields are work telephone number and home telephone number. I can set a max length for each field but is there anway to set a minimum length for each field so that a user cannot just enter '12' and skip to another field without putting in a complete work extension or home phone number?
    This topic has been closed for replies.

    5 replies

    January 4, 2008
    Sorry Forrest... but it's easier in JavaScript:
    1) Set up a js function like this ...
    function minium_length_check( ) {
    var myForm=document.getElementsByTagName("form"][0];
    var myField=myForm.telephone.value;
    var minimum = 5;
    if (myNbr.length >= minimum) {
    return;
    } else {
    alert( 'Telephone Numbers MUST be at least ' + minimum + ' characters in length!");
    myField.focus();
    myField.select();
    return
    }

    2) add an "onblur" event to the input field:
    <input type="text" name="telephone" size="10" maxlength="16" onblur="javascript:minimum_length_check();" />

    ... Just be certain to check the length a 2nd time when the data are submitted because the "onblur" event only fires after the viewer gets to - and attempts to leave - the field. But who is to say that they will ever actually visit that field?
    October 3, 2007
    The only other way is check it once the form has been submitted. That's a good solution, even if you use JavaScript. If it is that important, you should check it even after it has passed the JavaScript and been submitted. Take t from a person who has made that mistake.

    It's kind of like the old carpenters adage, "measure once, measure twice, cut". Here you should "check once" with JavaScript then "check twice" once the form has been submitted. Not sure where the "cut" comes into play?

    David Moore
    UpstateWeb
    Inspiring
    October 2, 2007
    MelissaW95 wrote:
    > Thanks Phil but I can't use javascript.

    can't = do not know how ?
    or
    can't = not allowed ?

    --

    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com
    October 2, 2007
    I was trying to avoid using javascript but it looks like I have no choice. So, any suggestions on using javascript to require a field to be a certain length?
    Inspiring
    October 3, 2007
    I don't use js often enough to get good at it. I do find google to be my freind though. A good search string would be javascript length.
    October 1, 2007
    Thanks Phil but I can't use javascript.
    October 1, 2007
    Will <cfinput message="Please enter a valid Phone Number" name="txt_phone" required="yes" type="text" validate="telephone"> work?
    October 1, 2007
    I tried that but it will not work for the work phone number since it is only a five digit extension.

    Is there a way to do a 'check length'?
    Participating Frequently
    October 1, 2007
    Javascript.

    Phil