www.webdeveloper.com
Results 1 to 4 of 4

Thread: Client side validation, Please check my logic

Hybrid View

  1. #1
    Join Date
    Nov 2012
    Posts
    4

    Client side validation, Please check my logic

    Javascript form onsubmit calls formval(). This works i know because i pass through the function it calls. The form and everything is fine, i can run server side php to do same job. It is something in the flow and i cannot see it. maybe someone else will spot what is wrong.

    The alert appears for a second and disappears. so i know the function is called, and i have placed alerts at the beginning of each major function and it appears, so it is something inside. i am teaching myself this stuff. formval() is at the bottom, logboxOn() is fine, it just displays hidden divs, i put it there for convenience so ignore that

    Hope someone can see something

    <script type="text/javascript">

    function logboxOn() {
    document.getElementById( 'login-box' ).style.visibility = 'visible';
    }

    function vemail(){
    var errstat = false;
    var emailrgx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
    if (document.tulogin.usname.value.search(emailrgx) == -1) {
    errstat = false;
    alert("Please enter a valid email address.");
    }else{
    errstat = true;
    }
    return errstat;
    }

    function valname1() {
    var errstat = false;
    var entname = document.tulogin.usname.value;
    if (entname == 'email@abc.com') {
    alert('email@abc.com is an invalid user');
    errstat = false;
    }else{
    errstat = true;
    }
    if (errstat == true ) {
    errstat = vemail();
    }
    return errstat;
    }

    function valpass() {
    var errstat = false;
    var taglen = document.tulogin.upasswd.value.length;
    var tagval = document.tulogin.upasswd.value;
    if( taglen < 6 || taglen > 15 ){
    errstat = false;
    alert('Password must be 6 to 15 characters');
    }else{
    if (tagval.match(/^[a-zA-Z0-9]+$/)) {
    errstat = false;
    alert('Invalid password entered');
    }else{
    errstat = true;
    }
    }
    return errstat;
    }

    function formval() {
    var errstat = false;
    if (valname1()) {
    if (valpass()) {
    errstat = true;
    alert('You found the lucky egg');
    }else{
    errstat = false;
    }
    }else{
    errstat = false;
    alert('Houston, we have a problem. Something is wrong, i know it is??');
    }
    if (errstat == -1) {
    window.location = 'index.html';
    }else{
    window.location = 'tu.html';
    }
    }

    </script>

    ________________________________________

    Roy Lister - http://www.reallymisfit.com
    ________________________________________
    Adelaide, South Australia
    all things Photography & Linux

  2. #2
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    Perhaps it's this line:
    Code:
    if (errstat == -1)
    which should perhaps be
    Code:
    if (errstat == false)
    since errstat is never -1.

  3. #3
    Join Date
    Nov 2012
    Posts
    4
    Quote Originally Posted by refreezed View Post
    Perhaps it's this line:
    Code:
    if (errstat == -1)
    which should perhaps be
    Code:
    if (errstat == false)
    since errstat is never -1.
    thanks for pointing out as i didn't notice. i tend to mix things a lot while working

    -1 always equals False because it can never be True in a Logical test. the code worked fine as i had tested all expressions

    cleaned it up the code a bit. The problem is in valpass(). the logic i have almost worked out now, it almost works. i am not being returned True from valpass(), the length test returns False so it has something to do with the regex. They are fussy, often need backslashing to pass things, but this one is from the Standards and the email one works (they are not unsimilar)

    this is where i am at now, i have also stripped it a bit to make it cleaner (i write bad code, but always clean it). still more to do

    Code:
    <script type="text/javascript">
    
    	function logboxOn() {
    		document.getElementById( 'login-box' ).style.visibility = 'visible';
    	}
    
    	function vemail(){
    		var errstat = false;
    		var emailrgx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
    		errstat = document.tulogin.usname.value.search(emailrgx);
    		return errstat;
    	}
    
    	function valname1() {
    		var errstat = false;
    		var entname = document.tulogin.usname.value;
    		if (entname == 'email@abc.com') {
    			errstat = false;
    			return errstat;
    		}
    		errstat = vemail();
    		return errstat;
    	}
    
    	function valpass() {
    		var errstat = false;
    		var taglen = document.tulogin.upasswd.value.length;
    		var alpnumrgx = '/^[a-zA-Z0-9]+$/';
    		if( taglen < 6 || taglen > 15 ){
    			errstat = false;
    			return errstat;
    		}
    		errstat = document.tulogin.upasswd.value.search(alpnumrgx);
    		return errstat;
    	}
    
    	function formval() {
    		if (valname1()) {
    			if (valpass()) {
    				window.location = 'tu.html';
    				return true;
    			}
    		}
    		window.location = 'index.html';
    		return false;
    	}
    
    </script>
    ________________________________________

    Roy Lister - http://www.reallymisfit.com
    ________________________________________
    Adelaide, South Australia
    all things Photography & Linux

  4. #4
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    The string.search() method returns the index of the match in the string, or -1 if no match is found.

    errstat = document.tulogin.upasswd.value.search(alpnumrgx);
    should be
    errstat = (document.tulogin.upasswd.value.search(alpnumrgx) != -1);

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles