Results 1 to 5 of 5

Thread: form valid

  1. #1
    Join Date
    Feb 2008
    Posts
    62
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default form valid

    hi ! i have any code form valid form with javascript . but this code only check empty fields . how to edit this code for Email check ( valid ) - User ( +6 ) - password ( +6 ) . only edit this code . thanks alot .
    Code:
    function checkform() {
            // First the normal form validation
            if(document.form.user.value=='') {  
              alert ('[Please complete the "required" field]');
              document.form.user.focus();
              return false;
              } 
    		  if(document.form.pass.value=='') {
              alert('Please complete the "required" field');
              document.form.pass.focus();
              return false;
              }
    		  if(document.form.mail.value=='') {
              alert('Please complete the "required" field');
              document.form.mail.focus();
              return false;
              }
    		  if(document.form.passr.value=='') {
              alert('Please complete the "required" field');
              document.form.passr.focus();
              return false;
              }
            if(document.form.dis.value=='') {
              alert('Please enter image');
              document.form.dis.value='';
              document.form.dis.focus();
              return false;
              }

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Check the following code

    Code:
    function validateEmail(elementValue){
    	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
      return emailPattern.test(elementValue);
    }
                
    function trim(str){
    	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
    }
    
    function checkform(){
    	// First the normal form validation
    	if (trim(document.form.user.value) == '') {
    		alert('[Please complete the "required" field]');
    		document.form.user.focus();
    		return false;
    	}else if (trim(document.form.user.value).length < 7) {
    		alert('User  name length should be greater than 6');
    		document.form.user.focus();
    		return false;
    	}				
    	if (trim(document.form.pass.value) == '') {
    		alert('Please complete the "required" field');
    		document.form.pass.focus();
    		return false;
    	}else if(trim(document.form.pass.value).length < 7){
    		alert('Please enter a password with a length not less than 7');
    		document.form.pass.focus();
    		return false			
    	}
    				
    	if (trim(document.form.mail.value) == '') {
    		alert('Please complete the "required" field');
    		document.form.mail.focus();
    		return false;
    	}else if (!validateEmail(document.form.mail.value)) {
    		alert('Please enter a valid email ID');
    		document.form.mail.focus();
    		return false					
    	}
    	
    	if (trim(document.form.passr.value) == '') {
    		alert('Please complete the "required" field');
    		document.form.passr.focus();
    		return false;
    	}
    	
    	if (trim(document.form.dis.value) == '') {
    		alert('Please enter image');
    		document.form.dis.value = '';
    		document.form.dis.focus();
    		return false;
    	}
    	return true;
    }
    In the above code I've introduced two JavaScript function one for validating email ID and another one for trimming a string (removing empty space from left and right side of a string). I've also changed your code a bit so that if the user enters space character in the form fields you can avoid it while validating the inputs.

  3. #3
    Join Date
    Feb 2008
    Posts
    62
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    no not work ! with this edit all checkform disable .
    NOTE : on submit is : return checkform()

  4. #4
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    I've checked my code and found that the changes I've made are working as it should. It is difficult to say what is the problem without seeing how you've made your page. Anyway I've made a demo page in which I've made one more changes in the checkform function (you missed it I think), there is no checking to match re-entered password I've added that too in the following code

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
            <title>Untitled Document</title>
            <style type="text/css">
            </style>
            <script type="text/javascript">
                function validateEmail(elementValue){
                    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
                    return emailPattern.test(elementValue);
                }
                
                function trim(str){
                    return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
                }
                
                function checkform(){
                    // First the normal form validation
                    if (trim(document.form.user.value) == '') {
                        alert('[Please complete the "required" field]');
                        document.form.user.focus();
                        return false;
                    }
                    else 
                        if (trim(document.form.user.value).length < 7) {
                            alert('User  name length should be greater than 6');
                            document.form.user.focus();
                            return false;
                        }
                    if (trim(document.form.pass.value) == '') {
                        alert('Please complete the "required" field');
                        document.form.pass.focus();
                        return false;
                    }
                    else 
                        if (trim(document.form.pass.value).length < 7) {
                            alert('Please enter a password with a length not less than 7');
                            document.form.pass.focus();
                            return false
                        }
    				if (trim(document.form.passr.value) == '') {
                        alert('Please complete the "required" field');
                        document.form.passr.focus();
                        return false;
                    }else if(trim(document.form.passr.value)!= trim(document.form.pass.value)){
    					alert('Passwords does not match');
    					document.form.passr.focus();
    					return false;
    				}
                    
                    if (trim(document.form.mail.value) == '') {
                        alert('Please complete the "required" field');
                        document.form.mail.focus();
                        return false;
                    }
                    else 
                        if (!validateEmail(document.form.mail.value)) {
                            alert('Please enter a valid email ID');
                            document.form.mail.focus();
                            return false
                        }
                    if (trim(document.form.dis.value) == '') {
                        alert('Please enter image');
                        document.form.dis.value = '';
                        document.form.dis.focus();
                        return false;
                    }
                    return true;
                }
            </script>
        </head>
        <body>
        	<form name="form" action="" method="get" onsubmit="return checkform();">
        		<label>User </label><input type="text" name="user" /><br/>
    			<label>Passwd </label><input type="password" name="pass" /><br/>
    			<label>Re-enter passwd </label><input type="password" name="passr" /><br/>
    			<label>Email </label><input type="text" name="mail" /><br/>
    			<label>Image Name </label><input type="text" name="dis" /><br/>
    			<input type="submit" value="save"/>
        	</form>
        </body>
    </html>
    Note that the name, action and method attributes of form tag can be changed according to your requirement. Save this code in your PC and browse and try to press the 'save' button you can clearly see that the checkform function invoked correctly.

  5. #5
    Join Date
    Feb 2008
    Posts
    62
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    now worked ! thanks man .
    Last edited by why not; 06-03-2008 at 08:43 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •