Jump to content

Error checking/Data Validation


jwwceo

Recommended Posts

This may be a Javascript question...but I am curious what the best way to validate data is? I mean things like making sure a phone number is correct, or that the double entry passwords match. I could do this is php but I seem like I would need a page refresh. I am looking for something that maybe throws one of those little javascript error boxes???

 

Thanks for any tips!!

 

James

Link to comment
Share on other sites

This is really easy with javascript

 

<script type="text/javascript">
function validate_required(field,alerttxt)
{
with (field)
{
	if (value==null||value=="")
	{
		alert(alerttxt);
		return false;
	}
	else 
	{
		return true;
	}
}
}

function field_size(field,limit,alerttxt){
with(field)

{
   if(value.length > limit){
   
   alert(alerttxt); return false;
   
   }
   else
   {
   
   return true;
   
   }

}

}
function validate_form(thisform)
{
with (thisform)
{
	if (validate_required(title,"Error Message:\n All fields must be filled out!")==false)
	{
		title.focus();
		return false;
	}
	else if(validate_required(comment,"Error Message:\n All fields must be filled out!")==false)
	{
	    comment.focus();
		return false;
	}
	else if(validate_required(swf,"Error Message:\n All fields must be filled out!")==false)
	{
	    swf.focus();
		return false;
	}
	else if(field_size(title,28,"Error Message:\n Title length is more than 23 chars!")==false)
	{
	    title.focus();
		return false;
	}
	else if(field_size(comment,200,"Error Message:\n Comment length is more than 200 chars!")==false)
	{
	  comment.focus();
	  return false;
	} 
	else if(field_size(password,50,"Error Message:\n password length is more than 50 chars!")==false)

	{

	   password.focus();
	   return false;
	} 	
 }
return true;
}
</script>

 

 

Heres an example of one I wrote.

 

 

 

Link to comment
Share on other sites

You must validate data when it reaches the server. You can validate data using javascript in the browser before it is submitted to the server as a service to your visitor.

 

When your php code validates the data, you need to build up an error message (most people use an array to hold multiple validation error messages.) Then if there were any validation errors, you output those along with the original form (and any existing data in the form fields so that it does not need to be entered again.)

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.