Jump to content

Form Validation


stulleman

Recommended Posts

Hello Forum!

 

I have a little problem. What I have is a registration page.

I want to validate the input of the user in real time.

I use JQuery.

$("#inputUsername").change(checkUsername);	$("#inputEmail").change(checkEmail);	$("#inputPassword").change(checkPassword);	$("#inputPasswordConfirm").change(checkPasswordConfirm);

I check if something was changed.

 

For example like this:

function checkUsername() {					$(this).popover({placement:"right", container:'body'});		var username = $(this).val();				if(username == '') {						$(this).data('bs.popover').options.content = 'Please enter a username';			$(this).popover("show");					} else if(username.length < 5 || username.length > 20) {					$(this).data('bs.popover').options.content = 'Please enter a username with 5-20 characters';			$(this).popover("show");					} else {						$(this).popover("destroy");					}			}

Now when the user submits the form, I thought I could use something like this:

$("#registerForm").submit(function( event ) {				if(!checkUsername || !checkEmail || !checkPassword || !checkPasswordConfirm) {			event.preventDefault();		}			});

Obviously the functions don't have return types. But even with it doesn't work.

With it doesn't work I mean that submit event isn't ignored even if there are errors in the form.

 

I might be doing something wrong with JavaScript as I'm not used to it!

 

Before someone suggests a plugin, I don't have enough control with them.

 

Thanks!

Link to comment
Share on other sites

Obviously the functions don't have return types.

That's one problem. If the function doesn't return anything then the return value will be undefined, so the if statement will never change. They should return true or false based on the outcome. Other than that, when you want to call a function you need to put parentheses after the name, just writing the name of the function is a reference but doesn't actually execute it without the parentheses.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...