更改文本字段边框颜色以及验证电子邮件的@

时间:2014-09-05 11:28:54

标签: javascript jquery css css3

下面是我的代码,它是一个客户端表单验证,只显示验证消息,但我希望它在显示验证消息时也将边框颜色更改为红色。我做了这个(输入#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>

3 个答案:

答案 0 :(得分:0)

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

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

或者更优选地,将无效形式分配给一个类,例如'无效'。然后你的CSS可以读取:

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

仅使用此边框呈现无效表单。

答案 1 :(得分:0)

  <style>
    .error,#register-form
    {
        border: solid 1px #ca4343
    }
  </style>

验证js添加新类'错误'你可以在这个类上应用css来显示边框

答案 2 :(得分:0)

在你的css中创建一个新类:

input.error {
  border:1px solid #ca4343;
}

.error是jquery验证插件的默认错误类

OP增加了在电子邮件地址中检查@的要求:

验证规则中的行email: true会检查电子邮件地址是否为有效的电子邮件地址,from the docs这似乎是为了检查@