gpt4 book ai didi

javascript - 更改文本字段边框颜色并验证电子邮件的@

转载 作者:太空宇宙 更新时间:2023-11-03 17:59:32 25 4
gpt4 key购买 nike

下面是我的代码,它是客户端表单验证,只显示验证消息,但我希望它在显示验证消息时也将边框颜色更改为红色。我这样做了(输入#register-form {border:1px solid #ca4343;})但是没有用。

此外,我还需要在需要验证的电子邮件中检查@

<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>

<script>

// When the browser is ready...
$(function() {

// Setup form validation on the #register-form element
$("#register-form").validate({

// Specify the validation rules
rules: {
name: "required",
gender: "required",
address: "required",
email: {
required: true,
email: true
},
username: "required",
password: {
required: true,
minlength: 5
}
},

// Specify the validation error messages
messages: {
name: "<br>Please enter your name",
gender: "<br>Please specify your gender",
address: "<br>Please enter your address",
email: "<br>Please enter a valid email address",
username: "<br>Please enter a valid username",
password: {
required: "<br>Please provide a password",
minlength: "<br>Your password must be at least 5 characters long"
}
},

submitHandler: function(form) {
form.submit();
}
});

});

</script>

<form action="./" method="post" id="register-form" novalidate="novalidate">

<div class="label">Name</div><input type="text" id="name" name="name" /><br />
<div class="label">Gender</div><select id="gender" name="gender" />
<option value="">Select One</option>
<option value="Female">Female</option>
<option value="Male">Male</option>
</select><br />
<div class="label">Address</div><input type="text" id="address" name="address" /><br />
<div class="label">Email</div><input type="text" id="email" name="email" /><br />
<div class="label">Username</div><input type="text" id="username" name="username" /><br />
<div class="label">Password</div><input type="password" id="password" name="password" /><br />
<div style="margin-left:140px;"><input type="submit" name="submit" value="Submit" /></div>

</form>

最佳答案

那是一种形式,而不是输入。试试这个:

form#register-form {border:1px solid #ca4343;}

或者更优选地,为无效表单分配一个类,例如'无效的'。然后你的 CSS 可以读作:

form.invalid {border:1px solid #ca4343;}

仅呈现带有此边框的无效表单。

关于javascript - 更改文本字段边框颜色并验证电子邮件的@,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25684888/

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