S. N
S. N

Reputation: 3949

JQuery-mobile validation!

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.

Upvotes: 3

Views: 4816

Answers (3)

Jaya Mayu
Jaya Mayu

Reputation: 17247

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.net/mayooresan/A3rvK/3/

Upvotes: 2

Balaji Kandasamy
Balaji Kandasamy

Reputation: 4506

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

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

Upvotes: 2

Chris
Chris

Reputation: 4471

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})$/

Upvotes: 2

Related Questions