English 中文(简体)
如果是电话,如何检查:使用jquery地区代码是否正确?
原标题: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. I restricted the input field to 4 chars. I need to check after the 4 chars are entered if the area code is correct.

What it should do. After entering 4 number, the script should check the following things. Does the entered number equals something like "00{number 2 digit only}" if it doesnt alert("please enter correct tel areacode");

I hope i could explain my problem clear. How can i do it with javascript or jquery?

最佳回答

是否“0”有效区域代码? 它有“00”,后面有两位数......但我假定这些代码为0,然后是10或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
  }
});

但我认为,模糊事件在这方面会更有用,因为如果他只打了三位数,上述功能就没有通知用户。 一旦重点移除投入箱,通知将尽快发出,而第四位数则不在此列。 因此,我的建议是:

$( 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
  }
});

http://www.ohchr.org。 我认为,你重新确认与贵方特有的某种区域守则。 如果你想要确认国际电话码,你不妨查看这个名单:http://en.wikipedia.org/wiki/List_of_country_questing_codes 。 - 有许多+XXX(或 00XXX)数字,这些数字符合你4个特点的长篇投入。 美国的+1(001)格式中的许多数字为t。 我认为,如果是+或00,然后是至少一位数位数,而不是零,那么你就应当检查。

/^(+|00)[1-9]/.test( input.value );
问题回答

与此类似:

$( #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(/00d{2}/.test(value)) {
        // Is valid
    } else {
        // Not valid
    }
});

I d 避免在不有效部分使用<代码>alert,因为这样每当用户发出钥匙时,就会给用户一个警示箱。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签