如何制作国家代码+601或01,如前面的国家静态,不允许用户插入字母。 [英] How do I make like country code +601 or 01 like make country static in the front and don't allow user to insert alphabet.
问题描述
如何使国家代码+601或01像前面的国家代码静态,并使+字符可选,不允许用户插入字母。示例 +601 23456789或 01 23456789。大胆的一个需要在前面看,好像用户可以插入任何一个。
我尝试过:
这是我尝试的但它只验证只允许数字和+字符,但字符+可以将它插入任何地方。
$(function(){
$('#hpno')。keypress(function(e){
var txt = String。 fromCharCode(e.which);
console.log(txt +':'+ e.which);
if(!txt.match(/ [+ 0- 9] /))
{
返回false;
}
});
});
(function(){
('#hpno')。 keypress(function(e){
var txt = String.fromCharCode(e.which);
console.log(txt +':'+ e.which);
if(!txt.match(/ [+ 0-9] /))
{
返回false;
}
});
});
你必须指定+
字符首先是可选的,后跟仅数字并匹配整个输入:
if (!txt.match(/ ^ \ +?[0-9] +
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;
}
});
});
(function(){
('#hpno').keypress(function(e){
var txt = String.fromCharCode(e.which);
console.log(txt + ' : ' + e.which);
if(!txt.match(/[+0-9]/))
{
return false;
}
});
});
You have to specify the+
character first being optional followed by digits only and matching the whole input:
if(!txt.match(/^\+?[0-9]+
这篇关于如何制作国家代码+601或01,如前面的国家静态,不允许用户插入字母。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!