Well I'm working on client side validation, it's still got a ways to go but for some reason my catch error is displaying

<form action ="" method="POST">
        <span id="tryErr"></span><br>
        <input type="text" id="username" placeholder="Username"><br>
        <span id="usernameErr"></span><br>
        <input type="text" id="email" placeholder="Email address"><br>
        <span id="emailErr"></span><br>
        <input type="password" id="password" placeholder="Password"><br>
        <span id="passwordErr"></span><br>
        <input type="submit" value="Submit" onclick="validation();">
    </form>    
function validation(e){
                event.preventDefault(e);         
var error = [], username = document.getElementById('username').value, email = document.getElementById('email').value, password = document.getElementById('password').value, filter = email.indexOf("@"), dot = email.lastIndexOf("."); try{ if(username == ""){ error = document.getElementById("usernameErr").innerHTML = "Please enter a username"; } if(email == ""){ error = document.getElementById("emailErr").innerHTML = "Please enter a valid email"; } if (filter<1 || dot<atpos+2 || dot+2>=x.length) { error = document.getElementById("emailErr").innerHTML = "Please enter a valid email"; } if(password == ""){ error = document.getElementById("passwordErr").innerHTML = "Please enter password"; } else if(username < 3){ error = document.getElementById("usernameErr").innerHTML = "Username must be longer than three characters"; } else{ console.log("I/'m a gazebo"); } } catch(error){ error = document.getElementById("tryErr").innerHTML = "Something went wrong try again later"; return false; } }

    Well I changed it a little, I thought it might be better to approach it like a jigsaw puzzle, I can't quite clear all my errors

    <script type="text/javascript">      
    
        function validateUsername(username, error){
            var username = document.getElementById("username").value,
            error = document.getElementById("usernameErr");
    
           if(username == null || username==""){   
            return error.innerHTML = "Username is required";
            return false;
           }
            else if(!username.match(/^[0-9a-z]+$/) || username.length < 3){
            return error.innerHTML = "Username must be alphanumeric and three characters long";
            return false;
           }
           else{
               return error.innerHTML = "";
               return true;
           }
        }
    
        function validatePassword(password, error){
            var password = document.getElementById("password").value,
            error = document.getElementById("passwordErr");
           if(password == null || password==""){
            return error.innerHTML = "Password is required";
            return false;
           }
            else if(password.length < 7){
            return error.innerHTML = "Password must be seven characters long";    
            return false;
           }
           else{
               return error.innerHTML = ""; 
               return true;
           }
        }
    
        function validateEmail(email, error){
            var email = document.getElementById("email").value,
            apos = email.indexOf("@"),
    	    dotpos = email.lastIndexOf("."),
            error = document.getElementById("emailErr");
    
            if(email == null || email==""){
            return error.innerHTML = "Email is required";
            return false;
           }
            if (apos < 1  ||  dotpos-apos < 2 || dotpos == (email.length - 1)){
                return error.innerHTML = "Please enter a valid email";
    		    return false;
    	    } 
    	    else{
                return error.innerHTML = "";
    		    return true;
    	    }
        }
    
    
        function confirmValidation(e){
            event.preventDefault(e);    
            if(!validateUsername()){
                return false;
            }
            if(!validatePassword()){
                return false;
            }
             if(!validateEmail()){
               return false;
            }
            else{
                //do something clever here
            }
        }
    
        </script>
    
    <form action ="" method="POST">
            <span id="tryErr"></span><br>
            <input type="text" id="username" placeholder="Username"><br>
            <span id="usernameErr"></span><br>
            <input type="text" id="email" placeholder="Email address"><br>
            <span id="emailErr"></span><br>
            <input type="password" id="password" placeholder="Password"><br>
            <span id="passwordErr"></span><br>
            <input type="submit" value="Submit" onclick=" return confirmValidation();">
        </form>    

      A couple random thoughts (and probably not all that important):

      You could shorten this a bit:

                  else if(!username.match(/^[0-9a-z]+$/) || username.length < 3){ 
      
      /* instead: */
                  else if(!username.match(/^[0-9a-z]{3,}$/)){ 
      

      And maybe change this:

              function confirmValidation(e){
                  event.preventDefault(e);    
      if(!validateUsername()){ return false; } if(!validatePassword()){ return false; } if(!validateEmail()){ return false; } else{ //do something clever here } } /* to this: */ function confirmValidation(e){ event.preventDefault(e);
      return validateUsername() && validatePassword() && validateEmail(); }

      (Returning false should stop the form from submitting.)

        NogDog;11063551 wrote:

        A couple random thoughts (and probably not all that important):

        You could shorten this a bit:

                    else if(!username.match(/^[0-9a-z]+$/) || username.length < 3){ 
        
        /* instead: */
                    else if(!username.match(/^[0-9a-z]{3,}$/)){ 
        

        Ahh so it is more or less the same as

        if (filter_var($_POST['username'], FILTER_VALIDATE_REGEXP, ["options" => ["regexp" => "/.{3,25}/"]]) === FALSE)
        

        In idea not syntax

        NogDog;11063551 wrote:

        And maybe change this:

                function confirmValidation(e){
                    event.preventDefault(e);    
        if(!validateUsername()){ return false; } if(!validatePassword()){ return false; } if(!validateEmail()){ return false; } else{ //do something clever here } } /* to this: */ function confirmValidation(e){ event.preventDefault(e);
        return validateUsername() && validatePassword() && validateEmail(); }

        (Returning false should stop the form from submitting.)

        Makes sense, probably quite important I seem to have trouble keeping things DRY

          Over all it looks like

          <script type="text/javascript">      
          
              function validateUsername(username, error){
                  var username = document.getElementById("username").value,
                  error = document.getElementById("usernameErr");
          
                 if(username == null || username==""){   
                  return error.innerHTML = "Username is required";
                  return false;
                 }
                  else if(!username.match(/^[0-9a-z]{3,}$/)){
                  return error.innerHTML = "Username must be alphanumeric and three characters long";
                  return false;
                 }
                 else{
                     return error.innerHTML = "";
                     return true;
                 }
              }
          
              function validatePassword(password, error){
                  var password = document.getElementById("password").value,
                  error = document.getElementById("passwordErr");
                 if(password == null || password==""){
                  return error.innerHTML = "Password is required";
                  return false;
                 }
                  else if(password.length < 7){
                  return error.innerHTML = "Password must be seven characters long";    
                  return false;
                 }
                 else{
                     return error.innerHTML = ""; 
                     return true;
                 }
              }
          
              function validateEmail(email, error){
                  var email = document.getElementById("email").value,
                  apos = email.indexOf("@"),
          	    dotpos = email.lastIndexOf("."),
                  error = document.getElementById("emailErr");
          
                  if(email == null || email==""){
                  return error.innerHTML = "Email is required";
                  return false;
                 }
                  if (apos < 1  ||  dotpos-apos < 2 || dotpos == (email.length - 1)){
                      return error.innerHTML = "Please enter a valid email";
          		    return false;
          	    } 
          	    else{
                      return error.innerHTML = "";
          		    return true;
          	    }
              }
          
          
              function confirmValidation(e){
                  event.preventDefault(e);    
                  return validateUsername() && validatePassword() && validateEmail();
              }
          
              </script>
          
          
          <form action ="" method="POST">
                  <span id="tryErr"></span><br>
                  <input type="text" id="username" placeholder="Username" onchange="validateUsername();"><br>
                  <span id="usernameErr"></span><br>
                  <input type="text" id="email" placeholder="Email address" onchange="validateEmail();"><br>
                  <span id="emailErr"></span><br>
                  <input type="password" id="password" placeholder="Password" onchange="validatePassword();"><br>
                  <span id="passwordErr"></span><br>
                  <input type="submit" value="Submit" onclick=" return confirmValidation();">
              </form>    

          I think I'm missing something, but not entirely sure..

            cluelessPHP;11063553 wrote:

            ...Makes sense, probably quite important I seem to have trouble keeping things DRY

            I'm just getting to the point where I really take it to heart. A couple of the Ruby devs I work with are sticklers about making separate methods for each separate task, especially if they have to do the same task in more than one place. Any function/method that's more than half a dozen lines or so becomes a code smell to them that it's time to break out some of its functionality into another method. 🙂

              I always thought code would be as long as it needed to be, of course keeping it as dry as possible but really, how long is a piece of string?

                It's the idea that a function/method should only do one thing (and do it well) -- which has the by-product of making automated unit tests more practical/manageable. Your entry-point method then mostly just calls other methods, which in turn may call other methods, and so on ("It's turtles all the way down") until you get to the small, focused methods that actually have to use base-language functions and operators. If the methods are well-named, it's easy to tell at a high level what the code is doing. The downside is that you sometime find yourself drilling down from method to method to method to find a bug -- but a good editor/IDE for the language/framework you're using can make that relatively easy (often by putting the cursor in the method name call and either using a shortcut key or right-click option). The everything-is-an-object nature of Ruby possibly makes that a bit more practical in some ways (speculation on my part...maybe it's just because that's what the Ruby/Rails community preaches [insists] on?).

                  Unless I'm wrong(which is possible...well most likely) it sounds like PHP patterns, right?

                    Hmm..."patterns" is usually used at a somewhat higher level, I think, describing a design approach to solving certain problems with approaches that have emerged as tried-and-true ways to solve that problem. For instance, the Singleton pattern might be used for a database class where you want all uses of that class within an app to use the same object instance, and therefore the same database connection. Within that class, you might then apply the idea of keeping each of its methods narrowly focused and concise.

                      Ah, I think I understand. I'm sure it'll be a long time before I touch ruby/rails, mostly I'm trying to use the rest of this year to improve writing code and fixing my logic, probably in January I'll start seriously looking for some kind of a job, right now I think I'd be wasting the time of any company I applied to

                        Write a Reply...