5

I am developing an app in which I am using HTML5 fields in my form. I want to trigger html5 field validation on button click for this purpose I have written this code

 <input type="button" id="btnContactSubmit" class="btn btn-success" value="Submit">

  $('#btnContactSubmit').click(function (e) {

            if ($('#aspnetForm')[0].checkValidity()) {
                e.preventDefault();
                if (flag) {
                    createListItem();
                }
            } else {
                 e.preventDefault();

            }
        });

button click is working fine but it doesnot showing error message on relevant field I want to show error message without submitting my form.

3
  • 1
    @arshad - Not sure required is really required on a button ? Commented Dec 26, 2014 at 11:32
  • @adeneo: :p my mistake, removed the "type='button'" Commented Dec 26, 2014 at 11:36
  • Why don't you use required in your input fields? <input required /> Commented Dec 26, 2014 at 11:37

1 Answer 1

2

There is no way to trigger the native tooltips other than submitting the form.

If the form is valid, prevent it from submitting, and if it's not valid, just submit it, and the native HTML5 validation will happen, but the form won't be submitted anyway.

You are preventing the default action in both conditions, so the validation never happens, do it like this, and it will

$('#btnContactSubmit').click(function (e) {
    if ($('#aspnetForm')[0].checkValidity()) {
        e.preventDefault();
        if (flag) {
            createListItem();
        }
    }
});

FIDDLE

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.