0

我的表单似乎没有验证或提交。它之前正在提交和验证,但是 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/

4

2 回答 2

1

您的代码中几乎没有错误。

  1. validateForm() 永远不会被调用
  2. 第 174 行:应该是else if (illegalChars.test(l))
  3. 第 113 行:缺少右括号

这个小提琴似乎现在可以工作了,http://jsfiddle.net/u5565/

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

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

告诉 jQuery 在提交表单时对其进行验证。

于 2013-05-18T14:54:15.893 回答
0

在您的小提琴中,您忘记method="POST"了表单标签

<form name = "NewUser" id = "your-form" method="POST"> 
于 2013-05-18T15:18:05.377 回答