Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
136
rated 0 times [  138] [ 2]  / answers: 1 / hits: 28635  / 11 Years ago, thu, september 5, 2013, 12:00:00

I am trying to handle form validation on button click. It is validating the form but not showing error.


can anyone help me in this?


<form id="the-form" action="#">
<input type="text" required="required" placeholder="Required text" />
<input type="email" required="required" placeholder="email" />
<button id="btn" type="button">Submit</button>
</form>

JavaScript:


$("#btn").on("click", function(){
if($("#the-form")[0].checkValidity())
{
alert('validated');
}
else
{
//show errors
return false;
}
});

http://jsfiddle.net/5ycZz/


More From » html

 Answers
-1

I've achieved this by doing steps below:



1) I'm having form:



<form>
<textarea required></textarea>
<input type=submit style=display:none;/>
</form>

<a>Some other button to trigger event</a>


2) Now we have to check if the form is filled correctly:



//this is <a> click event:
if (!$('form')[0].checkValidity()) {
$('form').find('input[type=submit]').click();
return false;
}


This will trigger form sending but won't send because there are errors ;)



It appears that html5 validation errors are displayed on input[type=submit] click :)



Hope will work for You too! :)


[#75880] Wednesday, September 4, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kenyamelinad

Total Points: 339
Total Questions: 85
Total Answers: 116

Location: Marshall Islands
Member since Sun, Aug 29, 2021
3 Years ago
;