<div>
<form>
<p>
I'm not a taco, but I got the sauce
</p>
<label for="input">
Please enter a number
<div class="error">
<div class="error-message">Enter a higher amount</div>
</div>
<input id="input" type="text" name="test" data-min="49" placeholder="Enter here" title="enter a price" />
</label>
<button type="submit">
Submit
</button>
</form>
</div>
$('form').on('validate', function() {
var input = $(this).find('input');
var value = $.trim(input.val());
var min = input.data('min');
var submit = $(this).find(':submit');
var error_message = '';
if (value == '') {
error_message = 'Please enter something';
} else if (!$.isNumeric(value)) {
error_message = 'Please enter a real number';
} else if (min && value < min) {
error_message = 'Please enter a higher number';
}
if (error_message) {
input.closest('label').addClass('invalid').find('.error-message').html(error_message);
// dynamic change of data-tip impossible to show via CSS
submit.addClass('disabled');
} else {
submit.removeClass('disabled');
input.closest('label').removeClass('invalid')
}
});
$('form').submit(function(e) {
$(this).trigger('validate');
if (!$(this).find(':submit').is('.disabled')) {
alert('ok');
}
e.preventDefault();
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.