最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How do I force user to enter specific pattern in input field using regex and javascript - Stack Overflow

programmeradmin6浏览0评论

I admit, I never explored into the regex albeit knowing its power. And its striking back to me. I am trying to make user enter pattern P99999999 into an input text where P is mandatory while 99999999 can take any digits(not letters). I'm triggering keyup, input field has uppercasing through styling and so far, this is what I have done (I know its too little).

inpt.value = $.trim( inpt.value );
if( inpt.value.indexOf( 'P' ) != 0 )
    inpt.value = inpt.value.replace(/[A-Z]/g, '');

After some exploring I e up with this

[P]+[0-9]+(?:\.[0-9]*)?

I do not know how to validate my input against this. This was generated by an online tool. I know it shouldn't be that hard. Any help.

I admit, I never explored into the regex albeit knowing its power. And its striking back to me. I am trying to make user enter pattern P99999999 into an input text where P is mandatory while 99999999 can take any digits(not letters). I'm triggering keyup, input field has uppercasing through styling and so far, this is what I have done (I know its too little).

inpt.value = $.trim( inpt.value );
if( inpt.value.indexOf( 'P' ) != 0 )
    inpt.value = inpt.value.replace(/[A-Z]/g, '');

After some exploring I e up with this

[P]+[0-9]+(?:\.[0-9]*)?

I do not know how to validate my input against this. This was generated by an online tool. I know it shouldn't be that hard. Any help.

Share Improve this question asked Jun 25, 2013 at 16:43 srijansrijan 1,5221 gold badge13 silver badges25 bronze badges 1
  • 2 Just remember that you still have to validate the input server side – aaronman Commented Jun 25, 2013 at 16:46
Add a ment  | 

4 Answers 4

Reset to default 4

Try this:

P[0-9]{8}

That's a P, and then any number eight times.

If a P followed by any number of digits is valid, try

P[0-9]+

That's a P, and then any number at least once.

To make sure that this is all they enter, add ^ and $ (as mentioned in the ments):

^P[0-9]+$

A ^ is 'start of input', and the $ is 'end of input'.

So, your final code might be something like:

<input type="text" />
<div class="fail" style="display: none;">Fail!</div>

and

$("input").keyup(function() {
    var patt=/^P[0-9]+$/g;
    var result=patt.test($(this).val());
    if (result) {
        $(".fail").hide();        
    } else {
        $(".fail").show();        
    }
});

http://jsfiddle/fuMg4/1/

Finally, make sure you check this on the server side as well. It's easy to bypass client-side validation.

This tool is pretty handy: http://gskinner./RegExr/

[updated to fix the stream input (hold key) issue]

here you go

regex [ it might look unconventional as there can be none (*) digits but think again, it is a live validation not the final result validation ]:

^P\d*$

code [edit: simplified]:

    $('input').on('change keyup keydown',function(){
        var txt = $(this).val();
        $(this).val(/^P\d*$/.test(txt)&txt.length<10?txt:txt.substring(0,txt.length-1));
    });

DEMO

Why don't you use a third party plugin, like jQuery Mask Plugin...

Requirements like this have been implemented ad nauseum in javascript. You're trying to recreate the wheel, yet again.

Use the pattern attribute

http://cssdeck./labs/n8h33j1w

<input type="text" required pattern="^P[0-9]+$" />

input:invalid {
  background: red;
}

input:valid {
  background: green;
}

Be aware that older browsers will need a polyfill (namely, IE), as this is a relatively new property.

发布评论

评论列表(0)

  1. 暂无评论