So the general idea is to

loop through each input field and check validation rules via true or false, right now I'm more looking for recommended reading than anything else, over all I think it should work something like this:

loop through inputs
compare validation rules
if validation rules fail do something

<script type="text/javascript"> 
<!--



function Valiation(input)
{

	var input = document.getElementsByTagName("input");

	function inputValidation()
	{

	//	var findlength = "cake is not a lie"; 

		var validInput = 4;

		result = (input > validInput) ? true : false; 

		if(result === true)
		{
			console.log("clever boy");
		}
		else if(result === false) 
		{
			console.log("ben dan laowai");
		}

		console.log(result);
	}


	console.log(input.length);
	input = Array.prototype.slice.call(input);

	input.forEach(inputValidation)
	{
		console.log("will it work?");
	}	


}

//--> 
</script> 
 <form method="POST"  name="myForm">
<input type="text" name="username" placeholder="Username" onblur="return Valiation()"><span id="error"></span><br>
<input type="text" name="email" placeholder="Email address" onblur="return Valiation()"><span id="error"></span>
<input type="submit" value="enter">
</form> 

    In example I know in php I could do something like

    
    <?php
    class Validate{
    	private $_passed = false,
    			$_errors =array(),
    			$_db = null;
    
    public function __construct(){
    	$this->_db = DB::getInstance();
    }
    
    public function check($source, $items = array()){
    	foreach($items as $item =>$rules){
    		foreach($rules as $rule => $rule_value){
    
    			$value = trim($source[$item]);
    			$item = escape($item);
    
    			if ($rule === 'required' && empty($value)){
    				$this->addError("{$item} is required");
    			} else if(!empty($value)){
    				switch($rule){
    					case 'min':
    						if(strlen($value) < $rule_value){
    							$this->addError("{$item} must be a minimum of {$rule_value} characters.");
    						}
    					break;
    					case 'max':
    					if(strlen($value) > $rule_value){
    							$this->addError("{$item} must be a maximum of {$rule_value} characters.");
    						}
    					break;
    					case 'matches':
    						if($value != $source[$rule_value]){
    							$this->addError("{$rule_value} must match {$item}");
    						}
    					break;
    					case 'unique':
    						$check = $this->_db->get($rule_value, array($item, '=', $value));
    						if($check->count()){
    							$this->addError("{$item} already take.");
    						}
    			break;
    				}
    			}
    		}
    	}
    
    	if(empty($this->_errors)){
    		$this->_passed = true;
    	}
    
    	return $this;
    }
    
    private function addError($error){
    	$this->_errors[] = $error;
    	}
    
    public function errors(){
    	return $this->_errors;
    	}
    
    	public function passed(){
    		return $this->_passed;
    	}
    }
    
    
    	$validate = new Validate();
    		$validation = $validate->check($_POST, array(
    			'username' => array(
    				'required' => true,
    				'min' => 2,
    				'max' => 20,
    				'unique' => 'users'
    			),
    			'password' =>  array(
    				'required' => true,
    				'min' => 6
    			),
    			'password_again' => array(
    				'required' => true,
    				'matches' => 'password'
    			),
    			'name' => array(
    				'required' => true,
    				'min' => 2,
    				'max' => 50
    			),
    
    	));

    I had assumed there would be something like that in JS too, no?

      Write a Reply...