简体   繁体   English

使用Jquery验证表单信息

[英]Validating form information with Jquery

I'm trying to validate user information on our registration form. 我正在尝试验证我们注册表格上的用户信息。 I'm relatively new to JQuery so im having troubles getting the script to actually run and check the information. 我对JQuery相对较新,因此无法使脚本真正运行并检查信息。 Right now im just testing to see if the Email Address field is empty. 现在,我只是在测试以查看“电子邮件地址”字段是否为空。 Once i can check for that i should be able to handle generating the rest of the error checking. 一旦我可以进行检查,我就应该能够处理其余的错误检查。

This is our registration.html file 这是我们的registration.html文件

 <html>
<head><title>Registration Form: jQuery</title>

<script type="text/javascript" src="script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="script/my_script.js"></script>
</head>
<body>

    <form id="myForm"  method="POST">
        Email Address: <input type="text" id="email"/><br />
        Password: <input type="password" id="pass"/><br />
        First Name: <input type="text" id="fName"/><br />
        Last Name: <input type="text" id="lName"/><br />
        <button id="submit">register</button>
    </form>

    <div id="ack"></div>

    <script src="registration.js"></script>

</body>

And this is the registration.js file I've been working on 这是我一直在处理的registration.js文件

$(document).ready(function(){
$("#submit").click(function(){
    if($("#email").val() = "")
    {
        alert("Email Address must not be empty.");
    }
    else
    {
        alert("Success");
    }

}); 
});

Any help would be appreciated. 任何帮助,将不胜感激。 If it matters, we have been using Adobe PhoneGap Build to generate a mobile version of our site and I believe we used JQuery to connect to our database. 如果重要的话,我们一直在使用Adobe PhoneGap Build生成我们网站的移动版本,并且我相信我们使用JQuery连接到我们的数据库。 Thanks! 谢谢!

EDIT Its fixed!It was a combination of my If statement being incorrect and not properly linking to the JQuery library. 编辑它的固定!这是我的If语句不正确且未正确链接到JQuery库的组合。 The console option of chrome helped greatly. chrome的控制台选项有很大帮助。 Thanks guys! 多谢你们!

You are simply missing one character to get this to work. 您只是缺少一个字符来使它起作用。

Here is a Fiddle to show it working 这是一个小提琴以显示它的工作原理

//incorrect
if($("#email").val() = "")

//correct
if($("#email").val() == "")

Essentially you were trying to assign an empty string to the val() of your selector. 本质上,您试图将空字符串分配给选择器的val()

The difference between = and == and === is a common thing to trip up people that are newer to JavaScript. ======之间的区别是绊倒JavaScript ===的常识。

=   //assignment operator (x = 10)
==  //comparison operator - just checks value 
    // ('1' == 1) is true
=== //comparison operator - checks type & value
    // ('1' === 1) is false

Your If statement is wrong. 您的If陈述有误。
Should be this: 应该是这样的:

if($("#email").val() == "") { 
   alert("Empty email!"); 
} else { 
  alert($("#email").val()); 
}

If you want to check all fields in one statement you can do this: 如果要在一个语句中检查所有字段,可以执行以下操作:

if($("#field1").val() == "" || $("#field2").val() == "") {
    alert("Empty fields in form!")
} else {
    alert("Sucess!");
}

Small example: http://jsfiddle.net/VGgvm/ 小例子: http//jsfiddle.net/VGgvm/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM