2012-05-14 200 views
0

为什么它不工作有什么错吧...Javascript验证不起作用?

<script language="JavaScript" type="text/javascript"> 


//function to check empty fields 

function isEmpty(strfield1, strfield2) { 
    //change "field1, field2 and field3" to your field names 

    strfield1 = document.forms[0].name.value 
    strfield2 = document.forms[0].email.value 

    //name field 

    if (strfield1 == "" || strfield1 == null || !isNaN(strfield1) || strfield1.charAt(0) == ' ') { 
    alert("Name is a mandatory field.\nPlease amend and retry.") 
    return false; 
    } 

    //EMAIL field 
    if (strfield2 == "" || strfield2 == null || !isNaN(strfield2) || strfield2.charAt(0) == ' ') { 
    alert(" Email is a mandatory field.\nPlease amend and retry.") 
    return false; 
    } 
    return true; 
} 


//function to check valid email address 

function isValidEmail(strEmail){ 
    validRegExp = /^[^@][email protected][^@]+.[a-z]{2,}$/i; 
    strEmail = document.forms[0].email.value; 

// search email text for regular exp matches 

    if (strEmail.search(validRegExp) == -1) { 
    alert('A valid e-mail address is required.\nPlease amend and retry'); 
    return false; 
    } 
    return true; 
} 


//function that performs all functions, defined in the onsubmit event handler 

function check(form)){ 
    if (isEmpty(form.field1)){ 
    if (isEmpty(form.field2)){ 
     if (isValidEmail(form.email)){ 
     return true; 
     } 
    } 
    } 
} 
return false; 
} 

</script> 

它没有做任何事情,我不明白这是怎么回事那里,在形式上我把这个太

<form onsubmit="return check(this);" action="sendquery.php" name="contquery"> 
+1

你应该使用jquery验证插件,将节省大量的时间和精力 – Dhiraj

+2

验证只有两个字段,我不认为这是很好的把插件或库你的网页。 – Jaiff

+1

你可以创建一个小提琴并给它一个链接? – Tamil

回答

3

第一眼:太多的括号如图@FishBasketGordo所以我不会重复

看一眼 - 你通过现场和不测试的字段值

三一眼:你不及格正确的名称功能

第四眼 - isEmpty在空时返回false。它应该返回true

我固定所有这些

DEMO HERE

完成页显示在那里发生的事情。更新做不显眼的事件处理的形式

<html> 
<head> 
<title>Validation</title> 
<script type="text/javascript"> 

// trim for IE 
if(typeof String.prototype.trim !== 'function') { 
    String.prototype.trim = function() { 
    return this.replace(/^\s+|\s+$/g, ''); 
    } 
} 

//function to check empty fields 

function isEmpty(objfld) { 
    var val = objfld.value; 
    if (val.trim() == "" || val == null) { 
    alert(objfld.name+" is a mandatory field.\nPlease amend and retry."); 
    objfld.focus(); 
    return true; 
    } 
    return false; 
} 

//function to check valid email address 

function isValidEmail(objEmail){ 
    var validRegExp = /^[^@][email protected][^@]+.[a-z]{2,}$/i; 
    var strEmail = objEmail.value; 
    if (strEmail.match(validRegExp)) return true; 
    alert('A valid e-mail address is required.\nPlease amend and retry'); 
    objEmail.focus(); 
    return false; 
} 

//function that performs all functions, defined in the onsubmit event handler 

function validate(form) { 
    if (isEmpty(form.name)) return false; 
    if (isEmpty(form.email)) return false; 
    return isValidEmail(form.email); 
} 


window.onload=function() { 
    document.getElementById("form1").onsubmit=function() { 
    return validate(this); 
    } 
} 

</head> 
<body> 
<form id="form1"> 
    Name:<input type="text" name="name" /><br/> 
    Email:<input type="text" name="email" /><br/> 
    <input type="submit" /> 
</form>  
</body> 
</html> 
+0

我虽然也是,但没有。四个开放的括号和四个紧密的括号。 – FishBasketGordo

+0

OP代码中有5个右括号......我会说这就是问题,代码本身......:P –

+0

看起来不错,但不起作用,并且有5个大括号关闭5th的功能是isValidEmail(strEmail) {大括号。 – Jaiff

2

大概主要原因是语法错误:

// Syntax error ----v 
function check(form)){ 
    if (isEmpty(form.field1)){ 
     if (isEmpty(form.field2)){ 
      if (isValidEmail(form.email)){ 
       return true; 
      } 
     } 
    } 
} 
// The return statement should be above the previous closing bracket 
// and the final closing bracket removed. 
return false; 
} 

在第一行有一个额外的关闭paren,并且有太多的右括号。如果你在FireBugChrome Developer Tools或类似的工具中打开它,它会自动告诉你这个。