gpt4 book ai didi

java - 使用 Jsp Servlet 中的 bootstrapValidator 远程检查数据库中的电子邮件可用性

转载 作者:行者123 更新时间:2023-12-01 10:10:02 25 4
gpt4 key购买 nike

我正在尝试使用 jsp 和 servlet 中的 bootstrapValidator 检查数据库中的电子邮件是否可用。但问题是,即使数据库中没有电子邮件,它也总是显示错误消息。

我的 Bootstrap 表单代码,

<div id="registration-form" class="modal fade" role="dialog">
<div class="modal-dialog registration-modal">
<div class="modal-content">
<form action="registerdone.jsp" method="post" class="form-horizontal" role="form"
id="registrationFormValidation">
<div class="modal-header">
<a class="close cross" data-dismiss="modal">x</a>
<h3>Register</h3>
</div>
<div class="modal-body">

<div class="form-group">
<label class="col-md-3 control-label">Full Name</label>
<div class="col-md-8">
<input type="text" class="form-control" id="name" name="name" placeholder="Your Name">
</div>
</div>
<!-- email -->
<div class="form-group">
<label class="col-md-3 control-label">Email</label>
<div class="col-md-8">
<input type="text" class="form-control" id="email" name="email" placeholder="Your Email">
</div>
</div>
<!-- password -->
<div class="form-group">
<label class="col-md-3 control-label">Password</label>
<div class="col-md-8">
<input type="password" class="form-control" id="password" name="password"
placeholder="Enter Password">
</div>
</div>
<!--confirm password-->
<div class="form-group">
<label class="col-md-3 control-label">Confirm Password</label>
<div class="col-md-8">
<input type="password" class="form-control" id="confirmpassword" name="confirmpassword"
placeholder="Confirm Password">
</div>
</div>
<!-- mobile -->
<div class="form-group">
<label class="col-md-3 control-label">Mobile</label>
<div class="col-md-8">
<input type="text" class="form-control" id="mobile" name="mobile"
placeholder="Enter Mobile">
</div>
</div>
</div>
<div class="modal-footer modal-footer-hidden-border">
<a href="#" class="btn btn-link" data-toggle="modal" data-target="#login" data-dismiss="modal">Already
Registered ? Login</a>
<button type="submit" value="submit" class="btn btn-success">Register
</button>
</div>
</form>
</div>
</div>
</div>

我的bootstrapValidator代码,

<script type="text/javascript">

$(document).ready(function () {
console.log("hiiiiiiii register validations");
var validator = $("#registrationFormValidation").bootstrapValidator({
fields: {
name: {
validators: {
notEmpty: {
message: 'The full name is required'
},
stringLength: {
min: 6,
max: 30,
message: 'The full name must be more than 6 and less than 30 characters long'
},
regexp: {
regexp: /^[a-zA-Z\s]+$/,
message: 'The full name can only consist of alphabetical and spaces'
}
}
},
email: {
message: "Email is required",
validators: {
notEmpty: {
message: "Please provide an email address"
},
stringLength: {
min: 6,
max: 35,
message: "Email must be between 6 and 35 characters long"
},
emailAddress: {
message: "Email address must be valid"
},
regexp: {
regexp: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
message: 'Not a valid email address'
},
remote: {
type: "POST",
url: 'RegistrationEmailCheck',
delay: 1000,
message: 'Email is already available "PLEASE LOGIN"'
}
}
}, //.email
password: {
validators: {
notEmpty: {
message: "Password is required"
},
stringLength: {
min: 8,
message: "Password must be 8 characters long"
},
different: {
field: "email",
message: "Email and Password must be different"
}
}
},
confirmpassword: {
validators: {
notEmpty: {
message: "Confirm Password field is required"
},
identical: {
field: "password",
message: "Confirm Password and Password must match"
}
}
},
mobile: {
validators: {
notEmpty: {
message: "Mobile Number is required"
},
numeric: {
message: "Not a valid phone number"
},
stringLength: {
min: 10,
max: 10,
message: "Only 10 digits are allowed"
},
regexp: {
regexp: /^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[789]\d{9}$/,
message: 'Enter a valid number'
}
}
}
}//.fields
});
});
</script>

我的servlet代码,

 import dbconnector.DBInfo;
import org.json.simple.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


@WebServlet(name = "RegistrationEmailCheck")
public class RegistrationEmailCheck extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
PrintWriter out = response.getWriter();
JSONObject json = new JSONObject();
String availEmail = request.getParameter("email");
System.out.println(availEmail);
String SQL = "SELECT email FROM login WHERE email='" + availEmail + "'";
Connection con = DBInfo.getConn();
Statement st;
ResultSet rs;
try {
st = con.createStatement();
rs = st.executeQuery(SQL);
if (!rs.next()) {
out.print(true);
json.put("valid", true);
System.out.println("true");
} else {
out.print(false);
json.put("valid", false);
System.out.println("false");
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
out.print(json);
out.close();
}
out.print(json);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}

感谢任何帮助。

最佳答案

不要使用 Bootstrap validator 的remote方法,而是使用ajax直接验证您的电子邮件。

$('#testSubmit').click(function() {

$.ajax({
type : 'post',
url : 'RegistrationEmailCheck',
data: { email: 'sample@test.com'},
success : function(data) {
alert(data);
console.log(data);
}
});
});

在 HTML 中,您也可以在同一元素中使用 onblur 函数。但这是非常令人沮丧的。所以我添加了按钮。

<button id="testSubmit" />

关于java - 使用 Jsp Servlet 中的 bootstrapValidator 远程检查数据库中的电子邮件可用性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36178682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com