简体   繁体   English

如何使用jquery检查Tel:区号是否正确?

[英]How to check If a Tel: area code is correct using jquery?

Hy I need to check if the given phone area code is correct. 我需要检查给定的电话区号是否正确。 i created a input field, user can insert a tel area code like 0044 oder 0090 and so on. 我创建了一个输入字段,用户可以插入电话区号,如00440090等。 I restricted the input field to 4 chars. 我将输入字段限制为4个字符。 I need to check after the 4 chars are entered if the area code is correct. 输入区号正确后,我需要检查输入的4个字符。

What it should do. 它应该做什么。 After entering 4 number, the script should check the following things. 输入4号后,脚本应检查以下内容。 Does the entered number equals something like "00{number 2 digit only}" if it doesnt alert("please enter correct tel areacode"); 如果alert("please enter correct tel areacode");没有alert("please enter correct tel areacode");是否alert("please enter correct tel areacode");是否等于"00{number 2 digit only}" alert("please enter correct tel areacode");

I hope i could explain my problem clear. 我希望我能清楚地解释我的问题。 How can i do it with javascript or jquery? 我该如何使用javascript或jquery?

Is "0000" a valid area code? “ 0000”是有效的区号吗? It has "00" followed by two digits... but I assume the codes are 00 then a number 10 or higher. 它有“ 00”后跟两位数字...但是我假设代码是00,然后是10或更高的数字。

$('input.phone').keyup( function(){
  var num = $(this).val(), i = parseInt(num, 10), valid = true;
  if(num.length==4 && (i<9 || i>99) ){
    //phone number is invalid
  }
});

But I think that blur event will be more useful here, because the above function wouldn't notify the user if he typed only three digits. 但是我认为模糊事件在这里会更加有用,因为如果用户仅输入三位数字,上述功能就不会通知用户。 Notification will appear as soon as the focus is moved aout of the input box, not as soon as the fourth digit was typed. 焦点将在焦点移出输入框后立即出现,而不是键入第四位后立即出现。 So my proposition is: 所以我的主张是:

$('input.phone').blur( function(){
  var num = $(this).val(), i = parseInt(num, 10), valid = true;
  if(num.length != 4 || i<9 || i>99 ){
    //phone number is invalid, notify the user
  }
});

edit : I thought you're validating some kind of area codes specific to your coutry. 编辑 :我以为您正在验证某种特定于您的国家的区号。 If you want to validate international calling codes you may wish to look at this list: http://en.wikipedia.org/wiki/List_of_country_calling_codes - there are many +XXX (or 00XXX) numbers and these won't fit into your 4 characters long input. 如果您想验证国际电话代码,则不妨查看以下列表: http : //en.wikipedia.org/wiki/List_of_country_calling_codes-有很多+ XXX(或00XXX)号码,这些号码不适合您的4个字符长输入。 And many numers aren't in +XX (00XX) format, like +1 (001) for USA. 而且许多数字不是+ XX(00XX)格式,例如美国的+1(001)。 I think you should just check if it's + or 00 followed by at least one digit other than zero and let it in. 我认为您应该只检查它是+还是00,然后是零以外的至少一位数字,然后输入即可。

/^(\+|00)[1-9]/.test( input.value );

Something like this should work: 这样的事情应该起作用:

$('#field').keyup(function() {
    var value = $(this).val();

    // Restrict length to 4 characters
    if(value.length > 4) {
        value = value.substring(0, 3);
        $(this).val(value);
    }

    // Test is value equals to "00{number 2 digit only}"
    if(/00\d{2}/.test(value)) {
        // Is valid
    } else {
        // Not valid
    }
});

I'd avoid using alert on the not valid part, as that would give the user an alert box every time he presses a key. 我会避免在无效部分上使用alert ,因为这会在用户每次按下按键时给用户一个警报框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM