gpt4 book ai didi

javascript - 如何验证按钮单击 jquery/javascript 上的登录表单?

转载 作者:行者123 更新时间:2023-11-28 17:26:56 25 4
gpt4 key购买 nike

如何在按钮单击时验证登录表单。当我单击按钮然后调用 javascript 函数按钮单击时,我需要首先验证表单,需要,然后单击按钮工作。required 不起作用。我想要在点击按钮之前进行验证。

<form id="loginform" class="form-vertical" method="post">
<div class="control-group normal_text"> <h3><img src="~/Content/img/logo.png" alt="Logo" /></h3></div>
<div class="control-group">
<div class="controls">
<div class="main_input_box">
<span class="add-on bg_lg"><i class="icon-user"> </i></span>
<input type="text" id="txtusername" class="form-control required" placeholder="Username" name="username" required />

</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<div class="main_input_box">
<span class="add-on bg_ly"><i class="icon-lock"></i></span>
<input type="password" id="txtpassword" class="form-control" name="password" placeholder="Password" required />

<br />

<label style="color:red;font-size:15px;display:none;" id="errormsg">Incorrect Username or Password</label>


</div>

</div>
</div>

<div class="form-actions">
<span class="pull-left"><a href="#" class="flip-link btn btn-info" id="to-recover">Lost password?</a></span>
<span class="pull-right"><input type="submit" value="Login" id="btnlogin" class="flip-link btn btn-success" /></span>
</div>
</form>

<script type = "text/javascript"
src = "https://code.jquery.com/jquery-1.12.4.js" >
</script>
<script type = "text/javascript" >
$(document).ready(function() {

$("#btnlogin").click(function () {

var user = new Object();
user.Username = $('#txtusername').val();
user.Password = $('#txtpassword').val();
if (user != null) {
$.ajax({
type: "POST",
url: "/Admin/Login",
data: JSON.stringify(user),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (recordcount) {
if (recordcount != null) {
alert(recordcount);
if (recordcount <= 0) {

document.getElementById("errormsg").style.display = 'block';

}
else {

document.getElementById("errormsg").style.display = 'none';
}


}
else {
alert("Something went wrong");
}
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
}

});
});
</script>

最佳答案

<html>

<head>
<title>a</title>
</head>

<body>

<form id="loginform" class="form-vertical" method="post">
<div class="control-group normal_text">
<h3>
<!-- <img src="~/Content/img/logo.png" alt="Logo" /> -->
</h3>
</div>
<div class="control-group">
<div class="controls">
<div class="main_input_box">
<span class="add-on bg_lg">
<i class="icon-user"> </i>
</span>
<input type="text" id="txtusername" class="form-control required" placeholder="Username" name="username" required />

</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<div class="main_input_box">
<span class="add-on bg_ly">
<i class="icon-lock"></i>
</span>
<input type="password" id="txtpassword" class="form-control" name="password" placeholder="Password" required />

<br />

<label style="color:red;font-size:15px;display:none;" id="errormsg">Incorrect Username or Password</label>


</div>

</div>
</div>

<div class="form-actions">
<span class="pull-left">
<a href="#" class="flip-link btn btn-info" id="to-recover">Lost password?</a>
</span>
<span class="pull-right">
<input type="submit" value="Login" id="btnlogin" class="flip-link btn btn-success" />
</span>
</div>
</form>

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js">
</script>
<script type="text/javascript">
$(document).ready(function () {
$("#loginform").submit(function (e) {
var user = new Object();
user.Username = $('#txtusername').val();
user.Password = $('#txtpassword').val();
if (user != null) {
$.ajax({
type: "POST",
url: "/Admin/Login",
data: JSON.stringify(user),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (recordcount) {
if (recordcount != null) {
alert(recordcount);
if (recordcount <= 0) {

document.getElementById("errormsg").style.display = 'block';

}
else {

document.getElementById("errormsg").style.display = 'none';
}


}
else {
alert("Something went wrong");
}
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
}
debugger
return false;
//e.preventDefault();
});
});
</script>

</body>

</html>

关于javascript - 如何验证按钮单击 jquery/javascript 上的登录表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51337545/

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