0

So what I need to have happen:

User types in 10 digits (only digits) and clicks “Submit” On submit – the user gets redirected to another landing page.

Here is what I’ve done…and its redirecting, but not really validating the 10 characters, or that they are digits. I have another script that does that, but not both together as they use different programming langauges.

<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script>
<script>
$(function() {
    $('#myform').submit(function() {
        var myField = $('#myInput').val();
        if (myField == '') {
            alert('10 digit code is required');
            return false;
        }
        // at this stage we know that the form is valid as the user
        // filled the required myField. Now we can redirect
        window.location.href = 'http://www.corel.com';
        return false;
    });
});

</script>

</head>

<body>
<form id="myform" name="form1" method="post" action="">
  <label for="button"></label>
  <label for="myInput"></label>
  <input name="myInput" type="text" id="myInput" value="" size="12" maxlength="10" />
  <input type="submit" name="button" id="button" value="Submit" />
</form>
J.Sims
  • 1
  • 1
  • 1
  • Since you redirect and throw the value away, it's not clear why it matters what the field value looks like. – Pointy Nov 04 '11 at 15:04

2 Answers2

3

You can check to see that a string is a 10-character string of digits like this:

if (/^\d{10}$/.test(someValue)) {
  // it is OK
}
else {
  // not OK
}
Pointy
  • 405,095
  • 59
  • 585
  • 614
0

replace your if with

if (isNaN(myField) || myField.length !== 10) {

EDIT: your best bet looks to be

if (/^\d{10}$/.test(someValue)) {

Which is Pointy's answer, so go with that :)

red-X
  • 5,108
  • 1
  • 25
  • 38
  • Note that `isNaN(" ")` (or a string of 10 spaces) is `false`. The "isNaN()" function is tricky, as it's about IEEE-794 "not-a-number" values, and not more generally about things that are not numbers. – Pointy Nov 04 '11 at 15:07
  • your right, isNaN checks for numbers and not just digits ie `0x034` qualifies as a number for it, ill update – red-X Nov 04 '11 at 15:10