我在让表单验证或提交时遇到问题

时间:2013-05-18 14:31:09

标签: javascript jquery html forms

我的表单似乎没有验证或提交。它之前是提交和验证,但Jquery错误消息并非全部同时显示,所以我必须编辑代码,现在它根本没有提交或验证。

这是我的JS:

function validateUserName(user)
{
    var u = document.forms["NewUser"]["user"].value
    var uLength = u.length;
    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if (u == null || u == "")
    {
        return "You Left the Username field Emptyyy";
    }
    else if (uLength <4 || uLength > 11)
    {
        return "The Username must be between 4 and 11 characters";

    }
    else if (illegalChars.test(u)) 
    {
        return "The Username contains illegal charectors men!";
    }
    else
    {
        return "";
    }
}

function validatePassword(pwd)
{
    var p = document.forms["NewUser"]["pwd"].value
    var cP = document.forms["NewUser"]["confirmPwd"].value
    var pLength = p.length;
    if (p == null || p == "")
    {
        return "You left the password field empty";
    }
    else if (pLength < 6 || pLength > 20)
    {
        return "Your password must be between 6 and 20 characters in length";
    }
    else if (p != cP)
    {
        return "The passwords do not match!"
    }
    else
    {
        return "";
    }
}

function validateEmail(email)
{
    var e = document.forms["NewUser"]["email"].value
    var eLength = e.length;
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (eLength == "" || eLength == null) 
    {

        return "You left the email field blank!";
    } 
    else if (e.match(illegalChars)) 
    {

        return "ILEGAL CHARECTORS DETECTED EXTERMINATE";
    } 
    else 
    {
        return "";
    }
}
function validateFirstName(fname)
{
    var f = document.forms["NewUser"]["fName"].value;
    var fLength = f.length;
    var illegalChars = /\W/;

    if(fLength > 20)
    {
        return "First Name has a max of 20 characters";

    }
    else if (illegalChars.test(f))
    {
        return "Numbers,letter and underscores in first name only";

    }
    else 
    {
        return "";
    }


}

function validateLastName(lName)
{
    var l = document.forms["NewUser"]["lName"].value;
    var lLength = l.length;
    var illegalChars = /\W/;

    if(lLength > 100)
    {
        return  "Last Name has a max of 100 characters";
    }
    else if (illegalChars.test(f))
    {
        $("#ErrorLname").text("Numbers,letter and underscores in last name only";
    }
    else 
    {
        return "";
    }


}

function validateForm()
{
/*

   valid = true;
    //call username function
    valid = valid && validateUserName();

    //call password function
    valid = valid && validatePassword();

    //call email function
    valid = valid && validateEmail();

    //call first name function
    valid = valid && validateFirstName();

    //call first name function
    valid = valid && validateLastName();

    return valid;
*/


    var error = "";
    //call username function
    error += "\n"+validateUserName();

    //call password function
    error += "\n"+validatePassword();

    error += "\n"+validateEmail();

    error += "\n" + validateFirstName();

    error += "\n" + validateLastName();

    if(error === ""){
        return true;
    }
    else{
         $("#ErrorUser").text(error);
         $("#ErrorEmail").text(error);
         $("#ErrorFname").text(error);
         $("#ErrorPassword1").text(error);
         $("#ErrorLname").text(error);
         return false;

    }

}

  $('#your-form').submit(validateForm);

小提琴:http://jsfiddle.net/cyKgD/

2 个答案:

答案 0 :(得分:1)

您的代码中没有错误。

  1. 永远不会调用validateForm()
  2. 第174行:应为else if (illegalChars.test(l))
  3. 第113行:缺少关闭括号
  4. 这个小提琴似乎现在正在运作,http://jsfiddle.net/u5565/

    确保您的页面中包含jQuery。代码行

    $(function() {
      $('#your-form').submit(function() {
         return  validateForm(); 
     });
    });
    

    告诉jQuery在提交表单时验证表单。

答案 1 :(得分:0)

在你的小提琴中,你忘记了表格标签上的method="POST"

<form name = "NewUser" id = "your-form" method="POST">