0

所以我需要发生的事情:

用户输入 10 位数字(仅数字)并单击“提交” 提交时——用户被重定向到另一个登录页面。

这是我所做的……以及它的重定向,但并没有真正验证这 10 个字符,或者它们是数字。我有另一个脚本可以做到这一点,但不能同时使用,因为它们使用不同的编程语言。

<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>
4

2 回答 2

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
}
于 2011-11-04T15:05:39.187 回答
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 :)

于 2011-11-04T15:05:07.437 回答