SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    String must be certain length

    Hi guys

    How can I check that my string is a certain length

    Here is my code

    PHP Code:
                    var inpObjs document.getElementsByName('licensenumber[]');
                    
    validChars = /^[a-zA-Z\d]/
                    for(
    i=0inpObjs.lengthi++){
                        if(!
    validChars.test(inpObjs[i].value)){

    if (
    inpObjs.length() != 16
                          
    alert("Please enter licence number for each person");
         return 
    false;
                        }
                    } 

  2. #2
    SitePoint Zealot Amit Yaron's Avatar
    Join Date
    Jan 2011
    Location
    Hod Haharon, Israel
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a better regular expression: /^[a-zA-Z\d]{16}$/
    \d - matches a digit
    $ - matches the end of the string
    {16} - means 16 occurrences
    Try the following code:
    Code:
    validChars=/^[a-zA-Z\d]{16}$/;
                    for(i=0; i < inpObjs.length; i++){
                        if(!validChars.test(inpObjs[i].value)){
                           alert("Please enter licence number for each person");
                           return false;
                        }
                    }
    Notes:
    inpObj.length is the size of the array retruned by "getElementsByName"
    inpObj[i] is the i'th elementh of the array inpObj.

  3. #3
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much


Bookmarks

Posting Permissions

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