Jump to content

Help with Form Validator


argrafic

Recommended Posts

i have the following validator:

 

function formValidator(){
// Make quick references to our fields
var nom = document.getElementById('nombre');
var pes = document.getElementById('peso');
var eda = document.getElementById('edad');
var sex = document.getElementById('sexo');
var hrt = document.getElementById('TOTAL');

// Check each input in the order that it appears in the form!
if(isAlphanumeric(nom, "Nombre es requerido.")){
	if(isNumeric(pes, "Peso es requerido y debe ser un número.")){
		if(isNumeric(eda, "Edad es requerido y debe ser un número.")){
			if(madeSelection(sex, "Sexo es requerido.")){
				if(validate_required(hrt, "El total de horas debe de sumar 24.")) {
						return true;
				}
			}
		}
	}
}


return false;

}

function isEmpty(elem, helperMsg){
if(elem.value.length == 0){
	alert(helperMsg);
	elem.focus(); // set the focus to this input
	return true;
}
return false;
}

function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression)){
	return true;
}else{
	alert(helperMsg);
	elem.focus();
	return false;
}
}

function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
	return true;
}else{
	alert(helperMsg);
	elem.focus();
	return false;
}
}

function isAlphanumeric(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
	return true;
}else{
	alert(helperMsg);
	elem.focus();
	return false;
}
}

function lengthRestriction(elem, min, max){
var uInput = elem.value;
if(uInput.length >= min && uInput.length <= max){
	return true;
}else{
	alert("Please enter between " +min+ " and " +max+ " characters");
	elem.focus();
	return false;
}
}

function madeSelection(elem, helperMsg){
if(elem.value == "Please Choose"){
	alert(helperMsg);
	elem.focus();
	return false;
}else{
	return true;
}
}

function emailValidator(elem, helperMsg){
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp)){
	return true;
}else{
	alert(helperMsg);
	elem.focus();
	return false;
}
}

function validate_required(field,alerttxt){
with (field)
{
	if (value != 24) {
		alert(alerttxt);
		return false;
	} else {
			return true
			}
}
}

 

i have an issue with the isAlphanumeric validation form, because when people enter a name with spaces it says that it sends teh message that the name fieldname is empty and should be filled.

 

how can i correct it so that it "reads" spaces?

 

thanks!

Link to comment
Share on other sites

thanks for the help guys...

 

but i found the solution... and it was pretty simple

 

function isEmpty(elem, helperMsg){
if(elem.value.length == 0){
	alert(helperMsg);
	elem.focus(); // set the focus to this input
	return true;
}
return false;
}

 

that part (in the same code) helped me validate what i wanted...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.