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 to the CF scene
    Join Date
    Nov 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form field validation - characters 1 and 4

    I am trying to validate a form field. The field can have a maximum of 4 characters. The first character is always the letter 'A', the fourth character can be either the letter 'A' or the letter 'B' but nothing else. I also need a mask on the field like this A _ _ _

    I have this code, but I am now stuck. Any help with this is much appreciated.

    <script language="JavaScript" type="text/javascript">
    <!--
    function Format(obj){
    string='A _ _ _ '
    val=obj.value.replace(/_/g,'');
    val=val.replace(/A/g,'');
    val=val.replace(/\s/g,'');
    val1=''
    for (zxc0=0;zxc0<val.length;zxc0++){
    val1+=val.charAt(zxc0)+' ';
    }
    if (val1.length<0){
    obj.value=val1+string.substring(val1.length,string.length);
    }
    else{
    val2=val1.substring(0,0)+'A '+val1.substring(6,0);
    obj.value=val2+string.substring(val2.length,string.length);
    }
    }

    //-->
    </script>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,920
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    <script language="JavaScript" type="text/javascript">
    <!--
    function Format(lstr){ // lstr is the variable passed to the function
    if (/^[A][A-Z0-9][A-Z0-9][AB]$/i.test(lstr.value)) {
    // match returns true, so proceed
    }
    else
    {
    // match returns false - retry or whatever??
    }
    }

    You say that the field can have a maximum of four characters (what do you mean? Alpha characters? Alpha-numeric characters? Any character??) and the fourth character must be A or B. What if the field has only three characters or fewer?

    The above regex requires a string of exactly four characters, the first must be A (or a), the next two can be A-Z, a-z or 0-9, the fourth chaarcter must be A or B (or a or b). If this is not exactly what you want please come back.

  • #3
    New to the CF scene
    Join Date
    Nov 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form field validation - characters 1 and 4

    Thank you Philip, that's exactly what I needed and it works a treat.

    Richard


  •  

    Posting Permissions

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