2013-05-07 104 views
0

我对javascript稍微陌生,我开始理解这种编码机制是如何工作的。我创建了一个包含多个字段的简单html表单。我正在使用JavaScript从现场获取数据并通过多种验证功能对其进行验证。下面的代码是JavaScript我目前使用:Javascript表单验证返回

function Validation() 
{ 
    submit_name = document.getElementById('txt_Name').value; 
    submit_surname = document.getElementById('txt_Surname').value; 
    submit_mobilenumber = document.getElementById('MobileNumber').value; 


    if(checkName(submit_name)) 
     { 
    if(checkSurname(submit_surname)) 
      { 
    if(checkMobile(submit_mobilenumber)) 
       { 
       } 
      } 
     } 
    return false; 
} 

我的问题是:在这种代码的情况下,主要功能(验证())会遍历所有的单个功能也一一?

如果例如检查名()函数返回假,将其它两个验证函数checkSurname()和checkMobile()运行或将程序停止在第一个?

的原因,我的问题是,毕竟验证功能都通过返回我要添加其他功能,以节省一切文件。但是,这只能在所有表单都通过验证后才能完成。提前感谢任何帮助。

+2

如果检查名()失败的用户,那么它不会进入if块本身,我希望我回答你的问题。 – dreamweiver 2013-05-07 09:47:13

+0

优秀,所以如果checkname失败,checksurname和checkmobile将无法正确运行? – progdoc 2013-05-07 09:48:25

+1

我建议你对字段进行个别验证,而不是嵌套条件。 – dreamweiver 2013-05-07 09:48:27

回答

3

我宁愿返回包含错误的字段中的每一个验证去,这样你可以显示哪些字段被错误地填写了

function Validation() { 
    var submit_name = document.getElementById('txt_Name').value, 
     submit_surname = document.getElementById('txt_Surname').value, 
     submit_mobilenumber = document.getElementById('MobileNumber').value, 
     errors = []; 

    if(!checkName(submit_name)) { 
     errors.push({ 
      id: 'txt_Name', 
      message: 'Name is required' 
     }); 
    } 

    if(!checkSurname(submit_surname)) { 
     errors.push({ 
      id: 'txt_Surname', 
      message: 'Surname is required' 
     }); 
    } 

    if(!checkMobile(submit_mobilenumber)) { 
     errors.push({ 
      id: 'MobileNumber', 
      message: 'Mobile number is required' 
     }); 
    } 

    return errors; 
} 

var errors = Validation(); 

if(errors.length === 0) { 
    // No errors, do your stuff 
} else { 
    // Loop errors and do something 
    for(var i = 0; i < errors.length; i++) { 
     // can mark the element with document.getElementById(errors[i].id) 
     alert(errors[i].message); 
    } 
} 
+0

这改善了我的答案,为每个元素添加一些错误消息。使用我的,你可以显示像'请填写所有输入'或类似的东西。这两种解决方案都不错,只取决于你想要的特定程度。很好的伙伴+1 – DaGLiMiOuX 2013-05-07 10:04:07

+0

是的,这真的取决于开发者的需求。当我验证表单时,我个人更喜欢这样的东西。 – thebreiflabb 2013-05-07 10:14:36

2

程序将停止在第一个方法返回false,因为所有这些都包含在彼此。因此,如果checkname()返回false,validation()将返回false,并为其他函数相同。

// if false, validate() returns false. if true, checksurname() called 
if(checkName(submit_name)) 
{ 
    // if false, validate() returns false. if true, checkMobile() called 
    if(checkSurname(submit_surname)) 
    { 
     // if false, validate() returns false 
     if(checkMobile(submit_mobilenumber)) 
     { 
     } 
    } 
} 
return false; 

虽然作为dreamweiver说,各个验证效果会更好:

if(checkname()&&checkmobile()&&checksurname()) 
{ 
    return true; 
} 
else 
{ 
    return false; 
} 
2

一个简单的解决方案(没有忽略你的其他检查功能)为您的验证应该是:

function Validation() 
{ 
    var booleanValidation = false; 
    var check_submit_name = checkName(document.getElementById('txt_Name').value); 
    var check_submit_surname = checkSurname(document.getElementById('txt_Surname').value); 
    var check_submit_mobilenumber = checkMobile(document.getElementById('MobileNumber').value); 

    if (check_submit_name === false || check_submit_surname === false || check_submit_mobilenumber === false) { 
     booleanValidaation = false; 
    } else if (check_submit_name === true && check_submit_surname === true && check_submit_mobilenumber === true) { 
     booleanValidaation = true; 
    } 

    return booleanValidation; 
} 
0

如果checkName(submit_name)将返回false,那么

if(checkName(submit_name))将变为if(false),因此条件将是错误的,因此if-condition代码将不会执行。并将继续执行。

这将适用于所有if条件。