简体   繁体   中英

Form validation using jquery method show() and hide()

I made a form for user details. I want to validate the form using jquery hide and show method. But I am doing something wrong as the required functionality is working. Where am I going wrong? I read other answers too on this topic but ain't getting the work done.

My form:

 <form id="uploadimageform" method="post" enctype="multipart/form-data">
                <input type="text" placeholder="first name" id="first" class="firstn" name="user[firstname]" /><br />
                <div class="firstname23">
                <p >Please fill the first name.</p>
                </div>
                <input type="text" placeholder="last name" id="last" class="lastn" name="user[lastname]" /><br />
                <div class="lastname23">
                    <p >Please fill the Last name.</p>
                </div>
                <input type="text" maxlength="10" placeholder="contact no." id="contact" name="user[contact_number]" onkeypress="return isNumberKey(event)" /><br />
                <input  type="address" placeholder="address1" id="addr1" name="user[address1]"/><br />
                <input  type="address" placeholder="address2" id="addr2" name="user[address2]"/><br />
                <input  type="address" placeholder="street" id="strt" name="user[street]"/><br />
                <input  type="address" placeholder="street1" id="strt1" name="user[street1]"/><br />
                <input  type="address" placeholder="city" id="city" name="user[city]"/><br />
                <input  type="address" placeholder="state" id="statee" name="user[state]"/><br />
                <input  type="address" placeholder="country" id="cntry" name="user[country]" /><br />
                <input  type="email" placeholder="email" id="email" class="emailn" name="user[email]" /><br />
                <div class="email23">
                    <p >Please fill the Email.</p>
                </div>
                <input  type="password" placeholder="password" id="paswrd" class="passn" pattern="^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$" name="user[password]" /><br />
                <div class="password23">
                    <p >Please fill the Password.</p>
                </div>

                <select name="user[gender]" id="gndr">
                    <option>Gender</option>
                    <option value="Male">Male</option>
                    <option value="Female">Female</option>
                </select><br />
                <input type="file" name="user_photo" id="user_photo" /><br>
                <div class="btn btn-primary" type="submit" id="addbutton" onclick="">Submit </div>
            </form>

My Jquery validation:

$(".firstname23").hide();
        $(".lastname23").hide();
        $(".email23").hide();
        $(".password23").hide();

        $("#addbutton").click(function(){
            alert($(".firstn").val())
        if ($(".firstn").val() == '') {
            $(".firstname23").show();
        }
        if ($(".lastn").val() == '') {
            $(".lastname23").show();
        }
        if ($(".emailn").val() == '') {
            $(".email23").show();
        }
        if ($(".passn").val() == '') {
            $(".password23").show();
        }
        });

You need to add required attribute on your input fields to make sure that they are filled by the user:

<input required type="text" placeholder="first name" id="first" class="firstn" name="user">

Only HTML5 supports the required attribute for form elements.

It's going to be needed to change your HTML type and make sure that your files starts with something like that:

<!DOCTYPE html>
<html>
...

You can remove then all slashes at the end of the tag from:

<input />

to:

<input>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM