Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How do I make like country code +601 or 01 like make country code static in the front,and make + character optional and don't allow user to insert alphabet. Example +60123456789 or 0123456789. The bold one need to be in the front see as if user can insert either one.

What I have tried:

This is what i had try but it only validate that allow only number and + character ,but the character + can insert it anywhere.
$(function(){
$('#hpno').keypress(function(e){
var txt = String.fromCharCode(e.which);
console.log(txt + ' : ' + e.which);
if(!txt.match(/[+0-9]/))
{
return false;
}
});
});
Posted
Updated 20-Aug-18 10:33am

You have to specify the + character first being optional followed by digits only and matching the whole input:
JavaScript
if(!txt.match(/^\+?[0-9]+$/))
Explained in detail:
^ Begin of string
\+ The + character (must be escaped with a backslash here)
? Matches the preceeding zero or one time
[0-9] Digits only
+ Matches the preceeding one or more times
$ End of string

An additional option might be limiting the number of digits to a minimum and maximum (here min. 6 and max. 16):
JavaScript
if(!txt.match(/^\+?[0-9]{6,16}$/))

I suggest to read about regular expressions. For JavaScript you might start at RegExp - JavaScript | MDN[^].
 
Share this answer
 
Comments
Member 13951173 20-Aug-18 21:24pm    
I have try both codes in my coding the first one it can't insert + character an the second example that you gave everything won't allow even numbers....:(
Jochen Arndt 21-Aug-18 3:04am    
The problem is not the regex but where you call it. My regex is for checking a complete input string.

I overlooked that you are using it within a keypress handler. That is not really useful for such checks because you can handle only single characters. While you can check those for being a + sign or a digit, you can't check if the actual keypress is the first character or not.

You might let your initial code in place to catch unallowed characters (which can be simpler and faster performed with an IF condition checking if e.what() is a digit or the + sign) and add a handler to check the whole input string. There you can use the regex from my solution.
Member 13951173 21-Aug-18 3:28am    
Means like this????


jQuery(function () {
$("#hpno").keyup(function () {
var VAL = this.value;

var phone = new RegExp(/^\+?[0-9]+$/);

if (phone.test(VAL)) {
alert('Great, you entered Phone Num');
}
});
});
Jochen Arndt 21-Aug-18 4:21am    
No. That is again for a single key.

The common method is to implement a validation function fo the whole form or for each field.

Example for a form field:

$('#hpno').on('input', function() {
 var input=$(this);
 // Apply regex to input here and act accordingly
});
[+0-9]

the RegEx only match 1 char, either a '+' or a digit. You should study RegEx more.

Just 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[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
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.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx:
Regexper[^]
 
Share this answer
 
v2

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