Jump to content

Problem with Validation


atrum

Recommended Posts

Ok, so I have a simple validation script that goes through and checks that the fields meet the forms requirements, and everything works. Sorta.

 

Once the process gets to the email validation it seems to work and the error is displayed, but if you click the button a second time the error goes away, click again and its back.

(you have to fill everything in up to the email point. I have not made this very fluid or friendly yet.)

What is causing this toggle effect.

 

Here is the page for people wanting to test it out.

 

http://dev.team-symbiosis.net/?p=login

Here is the JavaScript

(there is some jquery thrown in probably not needed)

//Register
	$('.error').hide(); //Hide the errors until needed.
	//Begin Validate Email.
	function valEmail(email){
		var pattern = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/g
		if(pattern.test(email)){
			return true;
		}else{
			return false;
		}
	}
	//end: Validate Email.
	function valReg(){
		var usr = $('#txt_username').val();
		var pas = $('#txt_password').val();
		var cpas = $('#txt_cpassword').val();
		var eml = $('#txt_email').val();
		var ceml = $('#txt_cemail').val();

		if(usr == "" || usr == null){
			$('#username_error').show();
			return false;
		}else{
			$('#username_error').hide();
		}

		if(pas == "" || pas.length < 5 || pas == null){
			$('#password_error').show();
			return false;
		}else{
			$('#password_error').hide();
		}

		if(cpas !== pas){
			$('#cpassword_error').show();
			return false;
		}else{
			$('#cpassword_error').hide();
		}

		if(valEmail(eml) !== true){
			$('#email_error').show();
			return false;
		}else{
			$('#email_error').hide();
		}

		if(eml !== ceml){
			$('#cemail_error').show();
			return false;
		}else{
			$('#cemail_error').hide();
		}
	}

	$('#btn_register').click(function() {
		valReg();	
	});
	//end: Register

Here is the markup.

 

<form id="register" name="register" method="post">
		<dl>
			<dt><label for="txt_username">Username:</label></dt>
				<dd><input type="text" name="txt_username" id="txt_username" /></dd>
				<label class="error" for="txt_username" id="username_error">This field is required.</label>
			</dl>
		<dl>
			<dt><label for="txt_password">Password:</label></dt>
				<dd><input type="password" name="txt_password" id="txt_password" /></dd>
				<label class="error" for="txt_password" id="password_error">The password is too short.</label>
		</dl>
		<dl>
			<dt><label for="txt_cpassword">Confirm Password:</label></dt>
				<dd><input type="password" name="txt_cpassword" id="txt_cpassword" /></dd>
				<label class="error" for="txt_cpassword" id="cpassword_error">The passwords do not match.</label>
		</dl>
		<dl>
			<dt><label for="txt_email">Email:</label></dt>
				<dd><input type="text" name="txt_email" id="txt_email" /></dd>
				<label class="error" for="txt_email" id="email_error">Invalid email address.</label>
		</dl>
		<dl>
			<dt><label for="txt_cemail">Confirm Email:</label></dt>
				<dd><input type="text" name="txt_cemail" id="txt_cemail" /></dd>
				<label class="error" for="txt_cemail" id="cemail_error">The emails do not match.</label>
		</dl>
		<dl><input type="button" id="btn_register" value="Complete" /></dl>
	</form><!--end: login-->

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.