Jump to content
Sign in to follow this  

Form Validation

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.



Share this post

Link to post
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.

Share this post

Link to post
Share on other sites

Join the conversation

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

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Sign in to follow this  

  • Create New...