为什么我的 Javascript 验证不起作用?

发布于 2024-09-04 03:51:14 字数 1477 浏览 5 评论 0原文

<script type='text/javascript'>
function formValidator(){
// Make quick references to our fields
var username = document.getElementByName('username');
var password = document.getElementByName('password');
var email = document.getElementByName('email');

// Check each input in the order that it appears in the form!
    if(isAlphanumeric(username, "Please only use letters and numbers for you username.")){
        if(lengthRestriction(username, 8, 12)){
            if(lengthRestriction(password, 6, 15)){
                if(emailValidator(email, "Please enter a valid email address")){
                            return true;
                }
            }
        }
    }


return false;
}


function isAlphanumeric(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
    return true;
}else{
    alert(helperMsg);
    elem.focus();
    return false;
}
}

function lengthRestriction(elem, min, max){
var uInput = elem.value;
if(uInput.length >= min && uInput.length <= max){
    return true;
}else{
    alert("Please enter between " +min+ " and " +max+ " characters");
    elem.focus();
    return false;
}
}

function emailValidator(elem, helperMsg){
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp)){
    return true;
}else{
    alert(helperMsg);
    elem.focus();
    return false;
}
}

即使用户名、密码和电子邮件文本框已定义名称属性,我的脚本仍无法正常工作。谢谢 :)。

<script type='text/javascript'>
function formValidator(){
// Make quick references to our fields
var username = document.getElementByName('username');
var password = document.getElementByName('password');
var email = document.getElementByName('email');

// Check each input in the order that it appears in the form!
    if(isAlphanumeric(username, "Please only use letters and numbers for you username.")){
        if(lengthRestriction(username, 8, 12)){
            if(lengthRestriction(password, 6, 15)){
                if(emailValidator(email, "Please enter a valid email address")){
                            return true;
                }
            }
        }
    }


return false;
}


function isAlphanumeric(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
    return true;
}else{
    alert(helperMsg);
    elem.focus();
    return false;
}
}

function lengthRestriction(elem, min, max){
var uInput = elem.value;
if(uInput.length >= min && uInput.length <= max){
    return true;
}else{
    alert("Please enter between " +min+ " and " +max+ " characters");
    elem.focus();
    return false;
}
}

function emailValidator(elem, helperMsg){
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp)){
    return true;
}else{
    alert(helperMsg);
    elem.focus();
    return false;
}
}

My script isn't working, even though the username, password and email text boxes have defined the name attribute. Thanks :).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

内心荒芜 2024-09-11 03:51:14

getElementByName 不是 document 对象的有效方法。您需要 getElementsByName,它将返回具有指定 name 属性的元素集合,或 getElementById() 它将返回具有指定 id 属性的单个元素。

// get the first element with name="username"
var username = document.getElementsByName('username')[0];

// get the first element with name="password"
var password = document.getElementsByName('password')[0];

// get the first element with name="email"
var email = document.getElementsByName('email')[0];

getElementByName isn't a valid method for the document object. You want getElementsByName, which will return a collection of elements with the specified name attribute, or getElementById() which will return a single element with the specified id attribute.

// get the first element with name="username"
var username = document.getElementsByName('username')[0];

// get the first element with name="password"
var password = document.getElementsByName('password')[0];

// get the first element with name="email"
var email = document.getElementsByName('email')[0];
七色彩虹 2024-09-11 03:51:14

没有 DOM 方法“getElementByName”,您可能希望考虑使用 getElementByIdgetElementsByName

There is no DOM method "getElementByName", you may wish to consider using getElementById or getElementsByName

仲春光 2024-09-11 03:51:14

document.getElementByName('用户名');返回名为 username 的数组或元素...所以也添加索引...即 document.getElementByName('username')[0];

它应该是这样的:

var username = document.getElementByName('username')[0];
var password = document.getElementByName('password')[0];
var email = document.getElementByName('email')[0];

document.getElementByName('username'); return array or elements with name username... so add index too... ie document.getElementByName('username')[0];

it should be like:

var username = document.getElementByName('username')[0];
var password = document.getElementByName('password')[0];
var email = document.getElementByName('email')[0];
落花随流水 2024-09-11 03:51:14

该方法称为 getElementsByName (请注意代码中缺少的 s),正如复数形式所示,它返回一个包含所有元素的数组匹配的元素。如果每个名称只有一个项目,则只需从数组中获取第一项即可。

现在,该方法返回 DOM 节点。如果您不知道节点是什么,只需将其视为 HTML 标签即可。您无法验证标签,您需要提取其 value 属性。

这些更改将反映如下(没有错误检查):

var username = document.getElementByName('username')[0].value;
var password = document.getElementsByName('password')[0].value;
var email = document.getElementsByName('email')[0].value;

建议您找到浏览器的 JavaScript 控制台,以便您可以获得有关语法错误的通知,或者更好的是,使用 Firefox + Firebug。

The method is called getElementsByName (please note the missing s in your code) and, as the plural form suggests, it returns an array with all the matching elements. If you only have one item per name, you can simply fetch the first item from the array.

Now, this method returns the DOM node. If you don't know what a node is, just think of it as the HTML tag. You can't validate a tag, you need to extract its value attribute.

These changes would get reflected as follows (with no error checking):

var username = document.getElementByName('username')[0].value;
var password = document.getElementsByName('password')[0].value;
var email = document.getElementsByName('email')[0].value;

Is suggest you find the JavaScript console of your browser so you can get notified about syntax errors or, even better, use Firefox + Firebug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文