validate the company registration number in JavaScript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KingIcode
    New Member
    • Nov 2015
    • 10

    validate the company registration number in JavaScript

    I want to validate company registration number in JavaScript which should be 14 characters long.

    EG : 1993/041545/23

    Currently this how I validate it.

    HTML CODE

    Code:
     
    <label >Registration Number:</label></br>
    <div class="warn"><span id="statusRegisternumber"></span></div>
     <input type="hidden" id="registernumber" class="hiddenId value=" Value="" />
    <input type="text" name="registernumber" id="registernumber" onkeyup="validate_registernumber(this.value);"   placeholder="Registration Number" title="Please provide a valid company registration number" <?php getpostvalue('registernumber');?>/></br>
              </p>

    JAVASCRIPT CODE

    Code:
    function validate_registernumber(registernumber)
    {
      var regRegisternumber =  /^[0-9]{14}$/;		
      if(regRegisternumber.test(registernumber) == false){
     		document.getElementById("statusRegisternumber").innerHTML = "Registration Number is not valid";
      }else{			            document.getElementById("statusRegisternumber").innerHTML = "You have entered a valid Registration number";
    	}
    }
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    your RegExp looks for 14 digits, not for 4 digits followed by a slash followed by 6 digits followed by a slash followed by 2 digits.

    Comment

    • KingIcode
      New Member
      • Nov 2015
      • 10

      #3
      Okay, please give me the best regex, i tried to do it in this order "4 digits followed by a slash followed by 6 digits followed by a slash followed by 2 digits. " but didn't work maybe, there is something m doing wrong

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        i tried to do it in this order "4 digits followed by a slash followed by 6 digits followed by a slash followed by 2 digits. " but didn't work maybe, there is something m doing wrong
        if you post your trial, I can tell you what was wrong. (I would stab a guess at unescaped characters)

        Comment

        • KingIcode
          New Member
          • Nov 2015
          • 10

          #5
          He is my regex:

          Code:
          /^[0-9/]{4}[0-9/]{6}[0-9/]{2}$/

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            [0-9/]{4} that looks for 4 characters, which can be either a digit or a slash. i.e. //// would match as would 8/15. in the end your Regexp is 2 characters short the requirement.

            Comment

            • KingIcode
              New Member
              • Nov 2015
              • 10

              #7
              I can put in 1224/145874/
              But i cant put in 224/145874/21

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                that’s correct.
                in the end your Regexp is 2 characters short the requirement.

                Comment

                • KingIcode
                  New Member
                  • Nov 2015
                  • 10

                  #9
                  This sort out my error
                  Code:
                  /^[0-9/]{4}[0-9/]{8}[0-9/]{2}$/
                  . Thanks buddy.

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    you can shorten that considerably: /^[\d/]{14}$/

                    Comment

                    • KingIcode
                      New Member
                      • Nov 2015
                      • 10

                      #11
                      That will be awesome, nice and short

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        and it will allow some values that are clearly not a registration number.

                        Comment

                        Working...