Click here to Skip to main content
15,942,144 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this combination " DE55680501010022061234 "
In this combination, first two letter is " DE " and followed by 20 numbers, and total of 22 length. How to check this format combination using JavaScript.
Posted
Updated 20-Jan-16 17:54pm
v2
Comments
dan!sh 20-Jan-16 23:53pm    
Is it always going to be DE<20 numbers> or first two characters might change?
vinu paul 20-Jan-16 23:57pm    
It is always going to be DE<20 numbers>. First two characters not change
Patrice T 21-Jan-16 0:04am    
Either use Regex or make a piece of code to do it.

It is expected that you would attempt to do what you need and share the code you have tried with the question. Having said that, here is something you can make use of:

You can choose from either regex method or non-regex method from following code.

C#
function Validate() {
           var inputString = 'DE55680501010022061234';
           // using regex
           var regEx = /DE\d{20}$/;
           if (regEx.test(inputString) == false) {
               alert('Not right!')
           }

           // without regex
           if (inputString.length != 22) {
                alert('Not right!')
           }

           if (inputString.charCodeAt(0) != 68 || inputString.charCodeAt(1) != 69) {
               // not D or E
               alert('Not right!')
           }

           for (var i = 2; i < 22; i++) {
               if (!(inputString.charCodeAt(i) > 47 && inputString.charCodeAt(i) < 58)) {
                   alert('Not right!')
               }
           }
       }
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 21-Jan-16 0:52am    
5d, but something bad happened to your code formatting...
—SA
dan!sh 21-Jan-16 1:02am    
Ah, fixed it. Thanks.
vinu paul 21-Jan-16 1:22am    
thank you very much.. got the answer :)
Try with below code:
JavaScript
<script>
function myFunction() {
    var str = "DE55680501010022061234";
    var pattern = /^DE[0-9]{20}$/g;
	if(str.match(pattern) == null)
	{
		alert("Failed");
	}else{
		alert("Success");
	}
}
</script>
 
Share this answer
 
Comments
aarif moh shaikh 21-Jan-16 1:42am    
Good one .. +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900