Form Validation using JavaScript

后端 未结 5 1283
-上瘾入骨i
-上瘾入骨i 2021-01-25 02:44

I\'m trying to validate a form using JavaScript but i\'m a bit stuck on displaying a message next to the field to say that \"This field is required\". How do I go about doing th

5条回答
  •  野性不改
    2021-01-25 03:21

    Your way of validation form fields is absolutely terrible, a lot of unneded and bloated code which is not optimized at all.

    I will strongly reccomend you use some sort of readyly available FormValidation script or have a look at this jQuery plugin
    Using jQuery your code can look like this (plus you also get the messages to appear next to field):

    $("#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
            }
        }
    })
    

提交回复
热议问题