Jump to content

Form field validation


MargateSteve
 Share

Recommended Posts

I have been toying around with form validation and have got to a sticking point in four areas that I am pretty sure are easier to do than I am finding!
 
What I have so far works fine - once you start typing in a field, if it is invalid, then there is a message and some extra styling that is removed once it is valid. These are the functions for the four fields so far (there will be more, some required, some not).
 

                  $('#username').on('keyup', function(){
			var valid = /^[a-zA-Z0-9_-]{3,16}$/.test(this.value) && this.value.length;
			$('#regUsername .regAlert').html((valid?'':'Not Valid'));
			if(!valid){
			  $("#regUsername").addClass('bg-danger');
			}else{
			  $("#regUsername").removeClass('bg-danger');
			}
		  });
			
			
		  $('#email').on('keyup', function(){
			var valid = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/.test(this.value) && this.value.length;
			$('#regEmail .regAlert').html((valid?'':'Not Valid'));
		  if(!valid){
			  $("#regEmail").addClass('bg-danger');
			}else{
			  $("#regEmail").removeClass('bg-danger');
			}
		  });
		  
		  $('#password').on('keyup', function(){
			var valid = (/^(?=.*\d)(?=.*[a-zA-Z])[0-9a-zA-Z]{6,}$/).test(this.value) && this.value.length;
			$('#regPassword .regAlert').html((valid?'':'Not Valid'));
		  if(!valid){
			  $("#regPassword").addClass('bg-danger');
			}else{
			  $("#regPassword").removeClass('bg-danger');
			  
			}
		  });
		  
		  $('#password_confirm').on('keyup', function(){
			var valid = (/^(?=.*\d)(?=.*[a-zA-Z])[0-9a-zA-Z]{6,}$/).test(this.value) && this.value.length;
			$('#regPassword2 .regAlert').html((valid?'':'Not Valid'));
		  if(!valid){
			  $("#regPassword2").addClass('bg-danger');
    		}else{
			  $("#regPassword2").removeClass('bg-danger');
			}
		  });

 
What I am trying to do next is, if any of these fields are not valid, for the submit button to be disabled I can do by including 

            if(!valid){
	       $('#registerSubmit').prop('disabled', true);
            }else{
	       $('#registerSubmit').prop('disabled', false);
	    }

in each function BUT if one is invalid, but the next is valid, then this overrides it and the button is clickable again. I have also tried setting  a variable to true/false and the same happens. Plus, if all are valid, once the button is clicked, it needs to check for empty (required) fields and only submit if all fields have data.

 

I am also guessing that they is a better way of writing this using a single keyup function and then placing each fields rules within that, but I have tried to wrap it all in a single function but got more in a mess.

 

So, what I am looking for help with (bearing in mind there will be more fields, radio's, checkboxes and selects in the full form) is

  1. How can I streamline all of the functions into one to save on repeated code and to make it simple to add more fields in future
  2. How can I disable the submit button if ANY of the validations have failed
  3. How can I check for any empty required fields on submit button click and not process the form if any are found
  4. How can I check that #password and #password_confirm match on keyup from #password_confirm

Thanks in advance for any advice

Steve

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.