为什么我的 Javascript 验证不起作用?
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
getElementByName
不是document
对象的有效方法。您需要getElementsByName
,它将返回具有指定name
属性的元素集合,或getElementById()
它将返回具有指定id
属性的单个元素。getElementByName
isn't a valid method for thedocument
object. You wantgetElementsByName
, which will return a collection of elements with the specifiedname
attribute, orgetElementById()
which will return a single element with the specifiedid
attribute.没有 DOM 方法“getElementByName”,您可能希望考虑使用
getElementById
或getElementsByName
There is no DOM method "getElementByName", you may wish to consider using
getElementById
orgetElementsByName
document.getElementByName('用户名');返回名为 username 的数组或元素...所以也添加索引...即 document.getElementByName('username')[0];
它应该是这样的:
document.getElementByName('username'); return array or elements with name username... so add index too... ie document.getElementByName('username')[0];
it should be like:
该方法称为
getElementsByName
(请注意代码中缺少的 s),正如复数形式所示,它返回一个包含所有元素的数组匹配的元素。如果每个名称只有一个项目,则只需从数组中获取第一项即可。现在,该方法返回 DOM 节点。如果您不知道节点是什么,只需将其视为 HTML 标签即可。您无法验证标签,您需要提取其
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):
Is suggest you find the JavaScript console of your browser so you can get notified about syntax errors or, even better, use Firefox + Firebug.