Pranava
Pranava

Reputation: 15

Clearing specific fields (not all) when a form is submitted

Im trying to clear only the password and confirm password fields when those are not matched. Im calling a function when the submit button is pressed which contains a password checking condition. How should I clear only password and confirmpassword inputs if that condition is true?

function checkmatch()
    {   
        var name = $("#username").val();
        var address = $("#address").val();
        var email = $("#emailid").val();
        var mobile = $("#phonenum").val();
        var password = $("#newPwd").val();
        var cnfirmPassword = $("#confirmPwd").val();

        if(password != cnfirmPassword)
        {
            alert("Passwords do not match.");
            return false;

        }
        else if((name==null || name == "") || (address==null || address == "") || (email==null || email == "") || (mobile=null || mobile == "") || (password==null || password == "") || (cnfirmPassword==null || cnfirmPassword == ""))
        {
            alert("Please fill all the required fields.");
            return false;
        }
        else
        {
            $.ajax(
                {
                    type: "POST",
                    url: "assmt1.php",
                    datatype: "html",
                    data:  $("#fm1").serialize(),
                    success: function(response)
                    {

                    }
                });
            alert("Your form has been submitted. Thank you!");  
        }               
    }

Upvotes: 0

Views: 30

Answers (2)

bdalina
bdalina

Reputation: 523

Is this what you mean?

    var password = $("#newPwd").val();
    var cnfirmPassword = $("#confirmPwd").val();

    if(password != cnfirmPassword)
    {
        alert("Passwords do not match.");
        $("#newPwd").val('').focus(); ///this will not focus because of alert
        $("#confirmPwd").val('');
        return false;

    }

Upvotes: 1

Get Off My Lawn
Get Off My Lawn

Reputation: 36289

All you need to do is call the same function you used to get the value.

Just pass empty string to val() like so:

$("#newPwd").val('');
$("#confirmPwd").val('');

Upvotes: 1

Related Questions