Javascript验证表单无效

时间:2013-09-22 22:06:41

标签: javascript validation

我在javascript中创建了一个带有验证的html SignUp页面。 这是验证码。

SignUp.html

< script type =“text / javascript”>

function validateForm() { 
var id = document.signup.LoginId;
var password = document.signup.LoginPassword;
var cpassword = document.signup.ConfirmPassword;
var city = document.signup.City;
var state = document.signup.State;
var number = document.signup.PhoneNumber;
var email = document.signup.Email;
var address = document.signup.Address;
var zipcode = document.signup.ZipCode;

if(id.value == "") {
    window.alert("Error: Username should be filled out.");
    id.focus();
    return false;
}
re = /^\w+$/;
if(!re.test(id.value)) {
    window.alert ("Error: Username must contain only letters, numbers and underscores.");
    id.focus();
    return false;
}

if(id.length < 6) {
    window.alert("Error: Username must contain at least 6 charecters.");
    id.focus();
    return false;
}
if(id.length > 12) {
    window.alert("Error: Username must not be greater than 12 charecters.");
    id.focus();
    return false;
}

if(password.value != "" && password.value == cpasswotd.value) {
    if(password.length < 6) {
        window.alert("Error: Password must contain at least 6 charecters.");
        password.focus();
        return false;
    }
    if(password.length > 12) {
        window.alert("Error: Password must not be greater than 12 charecters.");
        password.focus();
        return false;
    }
    if(password.value == id.value) {
        window.alert("Error: Password must be different from UserName.");
        password.focus();
        return false;
    }
    re = /[0-9]/;
    if(!re.test(password.value)) {
        window.alert("Error: Password must contain at least one number.");
        password.focus();
        return false;
    }
    re = /[a-z]/;
    if(!re.test(password.value)) {
        window.alert("Error: Password must contain at least one lowercase letter (a-z).");
        password.focus();
        return false;
    }
    re = /[A-Z]/;
    if(!re.test(password.value)) {
        window.alert("Error: Password must contain at least one uppercase letters (A-Z).");
        password.focus();
        return false;
    }
}else {
    window.alert("Error: Please check that you've entered and Confirmed your Password.");
    password.focus();
    return false;
}
window.alert("You have entered a valid password: "+password.value);
return true;



if(city.value == "") {
    window.alert("City must not be null.");
    city.focus();
    return false;
}

if(state.value == "") {
    window.alert("State must not be null.");
    state.focus();
    return false;
}

if(number.value == "") {
    window.alert("Phone number must not be null.");
    number.focus();
    return false;
}

if(number.length != 10) {
    window.alert("Phone number must be 10 digits.");
    number.focus();
    return false;
}

if (email.value == "")
{
    window.alert("Please enter a valid e-mail address.");
    email.focus();
    return false;
}

if (email.value.indexOf("@", 0) < 0)
{
    window.alert("Please enter a valid e-mail address.");
    email.focus();
    return false;
}

if (email.value.indexOf(".", 0) < 0)
{
    window.alert("Please enter a valid e-mail address.");
    email.focus();
    return false;
}

re = /^\w+$/
if(!re.test(address.value)) {
    window.alert("Error: Address must contain only letters, numbers and underscores.");
    address.focus();
    return false;
}

if(zipcode.value == "") {
    window.alert("Error: Zipcode must not be null.");
    zipcode.focus();
    return false;
}

if(zipcode.length > 6) {
    window.alert("Error: zipcode must not less than 6 digits.");
    zipcode.focus();
    return false;
}

} &LT; /脚本&GT;

验证表单无法正常运行。当我不提供用户名时,它会显示一个错误窗口。 如果我没有给出密码而且没有确认密码那么它也给出错误窗口,如果我只是给出任何用户名和密码并确认密码字段,(密码和确认密码文件不相同。)那么它是没有显示任何错误页面。它显示http状态500错误。

这是我的错误日志详细信息。

Sep 22, 2013 4:59:47 PM org.apache.catalina.core.AprLifecycleListener init    
INFO: The APR based Apache Tomcat Native library which allows optimal performance in 
production environments was not found on the java.library.path: 
.:/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java
Sep 22, 2013 4:59:47 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source'   to 'org.eclipse.jst.jee.server:WebsiteProj' did not find a matching property.
Sep 22, 2013 4:59:47 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Sep 22, 2013 4:59:47 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Sep 22, 2013 4:59:47 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1532 ms
Sep 22, 2013 4:59:47 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Sep 22, 2013 4:59:47 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.37
Sep 22, 2013 4:59:49 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Sep 22, 2013 4:59:49 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Sep 22, 2013 4:59:49 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1506 ms
Sep 22, 2013 5:00:07 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [SignUpServlet] in context with path [/WebsiteProj] threw exception
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:470)
at java.lang.Integer.parseInt(Integer.java:499)
at com.dao.SignUpServlet.doPost(SignUpServlet.java:50)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:680)

任何人都可以帮我解决这个问题。

谢谢。

0 个答案:

没有答案