I have a form validation where I insert errors below inputs to let users know they missed info. I initially encountered a bug where on click of submit multiple times with no info/incorrect emails in the form multiple error messages appeared.
I resolved this my setting an attribute of disabled to my button when the condition of the empty name of failed regular expression is met. However it has created a new problem - once the attribute of submit is set to disabled, it is permanent.
My javascript as it stands currently is below:
// Login validation
$('#customer_login').submit(function(e) {
// gets email input value
var emailinput = $('#customer_email').val();
// login page password value
var pwdinput = $('#customer_password').val();
if ($('input').length > 1) {
$('#customer_login :submit').attr('disabled', false);
}
// if it fails error and preventDefault otherwise submit
if (emailReg.test(emailinput) === false || emailinput === '') {
e.preventDefault();
$('#customer_login :submit').attr('disabled', true);
$('#customer_email').css('border-color', '#f84141');
$('#customer_email').after('<br><span class="field-error">Please enter valid email</span>');
}
// if password field is blank prompt user to put it in
if (pwdinput === '') {
e.preventDefault();
$('#customer_login :submit').attr('disabled', true);
$('#customer_password').css('border-color', '#f84141');
$('#customer_password').after('<br><span class="field-error">Please enter your password</span>');
}
});
As a potential remedy to my error, I try un-disabling when there is content in the input, which I try to do so here:
if ($('input').length > 1) {
$('#customer_login :submit').attr('disabled', false);
}
This does not work by itself in the global scope. I tried nesting this condition within each if statement that checks the value of the form input but that does not work, either. With all this said that leaves me with 3 questions:
Currently, when I run
$('input').length
in my console it is always returning48
, even when my inputs are empty. Why would an empty input return a value greater than zero?My test email is 25 characters, password is 16 so 25 + 16 = 41. Even with my test email and pass in the field the value of
$('input').length
is still 48. Why?running
$('input').val().length
in console as a check always returns0
- why would this be?
I have a jsfiddle, but am getting an error: {"error": "Please use POST request"}
despite having form action="POST"
. Changing to form method="POST"
returns:
{"error": "Shell form does not validate{'html_initial_name': u'initial-js_lib', 'form': <mooshell.forms.ShellForm object at 0x358a910>, 'html_name': 'js_lib', 'html_initial_id': u'initial-id_js_lib', 'label': u'Js lib', 'field': <django.forms.models.ModelChoiceField object at 0x3661710>, 'help_text': '', 'name': 'js_lib'}{'html_initial_name': u'initial-js_wrap', 'form': <mooshell.forms.ShellForm object at 0x358a910>, 'html_name': 'js_wrap', 'html_initial_id': u'initial-id_js_wrap', 'label': u'Js wrap', 'field': <django.forms.fields.TypedChoiceField object at 0x3661290>, 'help_text': '', 'name': 'js_wrap'}"}
https://jsfiddle.net/a4d5tLuh/2/
implementing both stackoverflow answers below were unsuccessful:
jsfiddle error {"error": "Please use POST request"} when submitting a form