0

次のコードを使用して 1 つの登録フォームを作成しました。

HTML:

<td class="fontstyle">
    Customer Name
    <span></span>
</td>
<td class="fontstyle">
    <input type="text" name="name" id="name">
</td>

jQuery:

$(document).ready(function () {
    if ($("#name").val('')) {
    }
    $('#name').focus(function () {
        if ($('#name').val() == 'Name Please') {
            $('#name').val("");
        }
    });
    $('#name').blur(function () {
        var nameRegex = /^[A-Za-z]+$/;
        // var fname = document.getElementById("name").value;
        var fname = $("#name").val();
        alert(nameRegex.test(fname));

        if (!(nameRegex.test(fname))) {
            $("#name").removeClass('greenborder').addClass('redborder');

        } else if (fname == " ") {
            $("#name").removeClass('greenborder').addClass('redborder');
        } else {
            $("#name").removeClass('redborder').addClass('greenborder');
            return false;
        }
    });
});

ここに私のjsfiddleがあります - http://jsfiddle.net/Mf6zJ/352/

名前のフォームを検証しました。trueしかし、とのアラートは必要ありませんfalse

Nameその近くにメッセージを表示したいthis name is not valid

私のjsfiddleをチェックして、それを行うための提案をしてください。

4

2 に答える 2

0

これが簡単な例です。

 Customer Name : <input type="text" name="name" id="name">
 <span id="errormsg" style="display:none;">Enter a Valid Name</span>
    <button type="submit" id="submit">Submit</button>


   <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
    $("#submit").click(function() {
var name = $("#name").val();

if(name.length < 1){
   $("#errormsg").css({"display":"block" , "color":"red"}); //add style you want
   return false;
 }else{
      $("#errormsg").css({"display":"none"});
}
    });
});

</script>

ワーキングデモ:http://jsfiddle.net/Mf6zJ/357/

于 2013-10-17T10:10:53.410 に答える
0

jqueryvalidationを使用した例を次に示します。

<script>
$.validator.setDefaults({
    submitHandler: function() { alert("submitted!"); }
});

$().ready(function() {
    // validate the comment form when it is submitted
    $("#commentForm").validate();

    // validate signup form on keyup and submit
    $("#signupForm").validate({
        rules: {
            firstname: "required",
            lastname: "required",
            username: {
                required: true,
                minlength: 2
            },
            password: {
                required: true,
                minlength: 5
            },
            confirm_password: {
                required: true,
                minlength: 5,
                equalTo: "#password"
            },
            email: {
                required: true,
                email: true
            },
            topic: {
                required: "#newsletter:checked",
                minlength: 2
            },
            agree: "required"
        },
        messages: {
            firstname: "Please enter your firstname",
            lastname: "Please enter your lastname",
            username: {
                required: "Please enter a username",
                minlength: "Your username must consist of at least 2 characters"
            },
            password: {
                required: "Please provide a password",
                minlength: "Your password must be at least 5 characters long"
            },
            confirm_password: {
                required: "Please provide a password",
                minlength: "Your password must be at least 5 characters long",
                equalTo: "Please enter the same password as above"
            },
            email: "Please enter a valid email address",
            agree: "Please accept our policy"
        }
    });

    // propose username by combining first- and lastname
    $("#username").focus(function() {
        var firstname = $("#firstname").val();
        var lastname = $("#lastname").val();
        if(firstname && lastname && !this.value) {
            this.value = firstname + "." + lastname;
        }
    });

    //code to hide topic selection, disable for demo
    var newsletter = $("#newsletter");
    // newsletter topics are optional, hide at first
    var inital = newsletter.is(":checked");
    var topics = $("#newsletter_topics")[inital ? "removeClass" : "addClass"]("gray");
    var topicInputs = topics.find("input").attr("disabled", !inital);
    // show when newsletter is checked
    newsletter.click(function() {
        topics[this.checked ? "removeClass" : "addClass"]("gray");
        topicInputs.attr("disabled", !this.checked);
    });
});
</script>
于 2013-10-17T10:11:20.307 に答える