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

javascript - JQuery-mobile validation! - Stack Overflow

programmeradmin2浏览0评论

I am using JQuery-mobile under eclipse. I have a form with 2 text fields, I want my 1st text field in the form to be able accept only numbers, so if the input is a char, or text or even empty, i want an error to appear. as for validation goes, I am using jquery validVal. I have included my codes `

<form id="ccform" method = "post">
        <table>
    <tr>
    <td><label for="cc">Card Number</label></td>
             <td><input name = "ccc" class="required" type = "text" id = "cc" maxlength="23" " ></td>
    </tr>
    <tr>
    <td>Card Holder Name</td>
    <td><input class="required" type = "text"></td>
    </tr>

    </table>

    </form>

and also:

<script>
    $("#ccform").validVal({
        customValidaton:{
            "cc": function ($field){
                var myexpr =/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/
                if(myexpr.test($field.val())) {return true;}
                else{return false;}
                }
            }
    });
    </script>

`

but I dont get any result, nothing... so any help would be appreciated.

I am using JQuery-mobile under eclipse. I have a form with 2 text fields, I want my 1st text field in the form to be able accept only numbers, so if the input is a char, or text or even empty, i want an error to appear. as for validation goes, I am using jquery validVal. I have included my codes `

<form id="ccform" method = "post">
        <table>
    <tr>
    <td><label for="cc">Card Number</label></td>
             <td><input name = "ccc" class="required" type = "text" id = "cc" maxlength="23" " ></td>
    </tr>
    <tr>
    <td>Card Holder Name</td>
    <td><input class="required" type = "text"></td>
    </tr>

    </table>

    </form>

and also:

<script>
    $("#ccform").validVal({
        customValidaton:{
            "cc": function ($field){
                var myexpr =/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/
                if(myexpr.test($field.val())) {return true;}
                else{return false;}
                }
            }
    });
    </script>

`

but I dont get any result, nothing... so any help would be appreciated.

Share Improve this question edited Jul 29, 2011 at 12:54 S. N asked Jul 29, 2011 at 12:42 S. NS. N 3,97913 gold badges45 silver badges71 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

You have a syntax error. Your regular expression should start and end with a /.

/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/

you can look at http://validval.frebsite.nl/examples.php.

See example 1, there is validVal validation available for number.

Why bother writing your own validation?? Use an existing plugin such as this one

You can do like this. first change the file as below. Note the Required number attribute.

<input name = "ccc" class="required number" type = "text" id = "cc" maxlength="23">

Then a simple $("#ccform").validate(); can do the magic.

full working example is here http://jsfiddle/mayooresan/A3rvK/3/

发布评论

评论列表(0)

  1. 暂无评论