[#] hm X_Shell Backd00r [#]

Current Path : /var/www/clients/client35/web46/web/buen-fin/js/
Upload File :
Current File : /var/www/clients/client35/web46/web/buen-fin/js/login.js

$(function(){
	$('#login').submit(function(e) {
	e.preventDefault();
		
	var va = new validarForm();
    ///va.prueba();
    
    va.setValor($("#txtName").val(),"#txtName","text","Usuario").evaluar();
    va.setValor($("#txtPass").val(),"#txtPass","text", "password").evaluar();
	

    if (va.getSuccess()) {

      $.post('controller/login.php', $(this).serialize(), function(data, textStatus, xhr){
        console.log(data)
        if( data == 'error') {
           $("#alert").html('Datos incorrectos');
        }else{
          window.location = data;
        }
        return false;
      });

      } else { 
      va.getErrors();
      }



	});
	
/*
	$.ajax({
		url: '/path/to/file',
		type: 'default GET (Other values: POST)',
		dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
		data: {param1: 'value1'},
	})
	.done(function() {
		console.log("success");
	})
	.fail(function() {
		console.log("error");
	})
	.always(function() {
		console.log("complete");
	});*/
	
});



validarForm = function(){

	var datos 		= [];
	var error 		= [];
	var password 	= [];

	this.limpiar = function(valor){
	 var valor = valor.replace(/^\s+/,'').replace(/\s+$/,'');
	 return valor;
	}

	this.setValor = function(valor,tag,type,name){	//Recupera los valores de las funciones a validar
		
		$(tag).removeAttr('style');
		datos['valor'] 	= this.limpiar(valor);
		datos['tag'] 	= tag;
		datos['type'] 	= type;
		datos['name']   = name;
		console.log(valor);
		console.log(tag);
		console.log(type);
		return this;
	}
	
	this.evaluar = function(){

		switch(datos['type']){
			case 'text':
					var string = /^([A-Za-zÑñáéíóúÁÉÍÓÚ0-9 ]+)$/;
					if(!string.test(datos['valor']) || datos['valor'] == ''){
						error.push(datos['tag'],datos['name']); 
						
					}
				break;
			case 'mensaje':
					var string = /^([A-Za-zÑñáéíóúÁÉÍÓÚ0-9 ]+){10}$/;
					if(!string.test(datos['valor']) || datos['valor'] == ''){
						error.push(datos['tag'],datos['name']); 
					}
				break;

			case 'email':
					var emailReg = /^([\da-z_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
					if( !emailReg.test( datos['valor']) || datos['valor'] == '' ) {

						error.push(datos['tag'],datos['name']); 
					}
				break;
		   case 'date':
					var date = /^\d{1,2}\/\d{1,2}\/\d{2,4}$/;
					if( !date.test( datos['valor']) || datos['valor'] == '' ) {

						error.push(datos['tag'],datos['name']); 
					}
				break;

			case 'password':
					if(datos['valor'].length <= 8){
						error.push(datos['tag']);
					}
					password.push(datos['valor']);
				break;

			case 'cpassword':
					if(datos['valor'] != password[0]){
						error.push(datos['tag']);
					}
				break;

			case 'num':
					var exp =  /^([0-9])*$/;
					console.log('valor = '+datos['valor']+' / tag = '+datos['tag']);
					if(!exp.test(datos['valor']) || datos['valor'] == ''){
						error.push(datos['tag'],datos['name']); 
					}
					
				break;

		}
		return this;
	}

     this.getSuccess = function (){

       if (error.length >= 1 ) {return false}else{return true
         
       }
     }

	this.getErrors = function(){ 
		console.log(error)
		$(error[0]).focus();
		$('#alert').html(error[1]+' incorrecto');
		$.each(error, function(index, val) {
			$(error[index]).css('border','1px solid #BE1D2C');
		});
		return error.length;
	}
}

Mr.hm X_Shell Backd00r 1.0, Coded By Mr.hm X_Shell Backd00r