Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Must be 12 characters
Must be numeric
e.g.:
aaaaaaaaaaaa: Invalid
0812888888aa: Invalid
081288128812: Valid
Thanx

What I have tried:

function numeric(text){
var numeric="1234567890";
for(var i=0; i < text.length; i++){
return false;
}
}

and
else if(numeric(phone)==false){ msg.innerHTML="harus angka";
}
Posted
Updated 12-Jun-17 8:08am
v2
Comments
Richard MacCutchan 12-Jun-17 12:32pm    
What is the question?
Richard Deeming 12-Jun-17 14:25pm    
In addition to validating the value, you should set the input type to tel; it doesn't do much in desktop browsers, but on phones and tables, it ensures that the correct keyboard shows up when the field is focussed, which makes it much easier to enter a number.

Browser support[^] is pretty much ubiquitous at this point. Any ancient browsers which don't support it will fall back to the text input type, so you won't break anything.

HTML5 forms input types[^]

This RegEx should do it:
^\d{12}$

A few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
 
Share this answer
 
For all this kind of validation the best tool is regexp...
Regular Expressions - JavaScript | MDN[^]
 
Share this answer
 
I'm not an experienced javascript developer, but I think this will do it:
JavaScript
function numeric(text){
    var numeric="1234567890";
    
    if (text.length != 12)
    {
      alert("invalid length !");
      return false;
    }
    
    for(var i=0; i < text.length; i++)
    {
      if (!numeric.includes(text[i]))
      {
      	 alert("invalid !");
         return false;
      }
    }
    
    alert("Valid !");
    return true;
}
 
Share this answer
 

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