By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

validate the company registration number in JavaScript

P: 10
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. <label >Registration Number:</label></br>
  3. <div class="warn"><span id="statusRegisternumber"></span></div>
  4.  <input type="hidden" id="registernumber" class="hiddenId value=" Value="" />
  5. <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>
  6.           </p>
  7.  

JAVASCRIPT CODE

Expand|Select|Wrap|Line Numbers
  1. function validate_registernumber(registernumber)
  2. {
  3.   var regRegisternumber =  /^[0-9]{14}$/;        
  4.   if(regRegisternumber.test(registernumber) == false){
  5.          document.getElementById("statusRegisternumber").innerHTML = "Registration Number is not valid";
  6.   }else{                        document.getElementById("statusRegisternumber").innerHTML = "You have entered a valid Registration number";
  7.     }
  8.  
Nov 10 '15 #1

✓ answered by Dormilich

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

Share this Question
Share on Google+
11 Replies


Dormilich
Expert Mod 5K+
P: 8,639
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.
Nov 10 '15 #2

P: 10
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
Nov 10 '15 #3

Dormilich
Expert Mod 5K+
P: 8,639
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)
Nov 10 '15 #4

P: 10
He is my regex:

Expand|Select|Wrap|Line Numbers
  1. /^[0-9/]{4}[0-9/]{6}[0-9/]{2}$/
Nov 10 '15 #5

Dormilich
Expert Mod 5K+
P: 8,639
[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.
Nov 10 '15 #6

P: 10
I can put in 1224/145874/
But i cant put in 224/145874/21
Nov 10 '15 #7

Dormilich
Expert Mod 5K+
P: 8,639
thatís correct.
in the end your Regexp is 2 characters short the requirement.
Nov 10 '15 #8

P: 10
This sort out my error
Expand|Select|Wrap|Line Numbers
  1. /^[0-9/]{4}[0-9/]{8}[0-9/]{2}$/
. Thanks buddy.
Nov 10 '15 #9

Dormilich
Expert Mod 5K+
P: 8,639
you can shorten that considerably: /^[\d/]{14}$/
Nov 10 '15 #10

P: 10
That will be awesome, nice and short
Nov 10 '15 #11

Dormilich
Expert Mod 5K+
P: 8,639
and it will allow some values that are clearly not a registration number.
Nov 10 '15 #12

Post your reply

Sign in to post your reply or Sign up for a free account.