CodeCharge Studio
search Register Login  

Visual PHP Web Development

Visually Create Internationalized Web Applications, Web Reports, Calendars, and more.
CodeCharge.com

YesSoftware Forums -> Archive -> GotoCode Archive

 Custom VALIDATION help please!

Print topic Send  topic

Author Message
xnet
Posted: 04/28/2002, 3:55 PM

Hello!

I need a little help from a more experimented CC developer.
How can I validate the following type of fields:
- email
- numbers
- letters
- date
- credit card (preliminary of course)

and where do I insert the code?

If you have PHP snippets for my requests they will be very much appreciated.
Nicole, Alex, you are my favorites!!!

Thanks.
Nicole
Posted: 04/29/2002, 6:36 AM

Hello,
I'm not quite sure about credit cards validation but as for the rest I think you can use JavaScript (or other client side) code for this purposes. Search on hotscripts.com for ready solutions and just incorporate them into your project.
jjtoubia
Posted: 04/29/2002, 7:02 AM

Hello,

When creating your CC page, in the forms properties be sure to click on "check validation". Now on each field's properties that you want validated, check the box that says something like "validate". Now that that is done, generate your page. I always generate my pages and then do any modifications in an external app like PHPed but that is up to you on how you do it. Make sure all the fields that you want have been called in.

Example:
//-------------------------------
// Load all form fields into variables
//-------------------------------
$fldemail = get_param("email");
$fldnumbers = get_param("numbers");
$fldletters = get_param("letters");
$flddatemonth = get_param("datemonth");
$flddateday = get_param("dateday");
$flddateyear = get_param("dateyear");

Now that you now your variables are there, on to validating them. We are going to use CC's default validation rules and modify them to what you need.

This is CC's default error checking
//-------------------------------
// Validate fields
//-------------------------------
if($sAction == "insert" || $sAction == "update")
{
if(!strlen($fldemail))
$sYourFormErr .= "The value in field email: is required.<br>";

if(!strlen($fldnumbers))
$sYourFormErr .= "The value in field numbers: is required.<br>";

if(!strlen($fldletters))
$sYourFormErr .= "The value in field letters: is required.<br>";

if(!strlen($flddatemonth))
$sYourFormErr .= "The value in field datemonth: is required.<br>";

ETC. ETC...

It's nice and all but only checks its existense. This is what you would want to change it too. NOTE: IF YOU ARE CUTTING AND PASTING THIS, BE SURE TO CHANGE THE ERROR VARIABLE TO YOUR FORMNAME. $sYourFormErr should be $s(your form name)Err.

//-------------------------------
// Validate fields
//-------------------------------
if($sAction == "insert" || $sAction == "update")
{
if(!eregi ("^([[:alpha:]]|_|\.|-)+@([[:alnum:]]|\.|-)+(\.)([a-z]{2,4})$", $fldemail)) // checks whether $fldemail is a valid email address
$sYourFormErr .= "Please enter a valid email. <br>";

if(ereg ("([0-9])", $fldnumbers)) // makes sure all numbers
$sYourFormErr .= "Please enter only numbers from 0-9.<br>";

if(!eregi ("^([[:alpha:]]|-|')+$", $fldletters)) // Checks to see if it is all letters
$sYourFormErr .= "Please enter only letters in the letter field.<br>";

if(!checkdate ($flddatemonth, $flddateday, $flddateyear)) // Checks to see if it is a valid date
$sYourFormErr .= "Please enter a valid date.<br>";

I think that is it. Sorry this message got too long. If you need anything explained furthur, I post it and I will try to help.
Brent
Posted: 04/29/2002, 7:07 AM

>>I'm not quite sure about credit cards validation but as for the rest I think you can use
>>JavaScript (or other client side) code for this purposes. Search on hotscripts.com for ready
>>solutions and just incorporate them into your project.

There is a problem with using Javascript for validation. If the user has
Javascript turned off, it won't validate the data. :(

The user can do this accidentally or even deliberately in order to get invalid
data into your database. Validation must be one on the server. You can't let
the user decide whether or not the data gets validated.

jjtoubia
Posted: 04/29/2002, 8:01 AM

OOPS sorry, amde a mistake on the numbers validation. I forgot the "!" before "ereg". It should be:

if(!ereg ("([0-9])", $fldnumbers)) // makes sure all numbers
$sYourFormErr .= "Please enter only numbers from 0-9.<br>";
xnet
Posted: 04/29/2002, 4:04 PM

Thank you for your very documented response.

Now there's a tough one: what if I don't want to have a long list of validation messages under form title and instead I want only the following message:
"Please fill in correctly the fields marked in red" .
Obviously the invalid fields caption style should change color to red.

Any idea, please?
jjtoubia
Posted: 04/29/2002, 8:04 PM

Hi!

Okay, to create the error messages will be some work. Hope you are ready to do it.

Since the captions generated in CC are not variables, we are going to have to make the captions variables so that we can make them red when the appropriate error is applied.

First off, lets create the new validation segment so that we only have one error message at the top or wherever and an array to let us know which field is incorrect and therefor should be red.


//-------------------------------
// Validate fields
//-------------------------------
if($sAction == "insert" || $sAction == "update")
{
if(!eregi ("^([[:alpha:]]|_|\.|-)+@([[:alnum:]]|\.|-)+(\.)([a-z]{2,4})$", $fldemail)) // checks whether $fldemail is a valid email address
$sYourFormErr = "Please fill in correctly all the fields marked in red. <br>"; //removed the period before the = symbol so that the error message doesn't concantate itself with other error messages
$capErr["email"] = TRUE;

if(!ereg ("([0-9])", $fldnumbers)) // makes sure all numbers
$sYourFormErr = "Please fill in correctly all the fields marked in red.<br>";
$capErr["numbers"] = TRUE;

if(!eregi ("^([[:alpha:]]|-|')+$", $fldletters)) // Checks to see if it is all letters
$sYourFormErr = "Please fill in correctly all the fields marked in red.<br>";
$capErr["letters"] = TRUE;

if(!checkdate ($flddatemonth, $flddateday, $flddateyear)) // Checks to see if it is a valid date
$sYourFormErr = "Please fill in correctly all the fields marked in red.<br>";
$capErr["date"] = TRUE;

Now we have to make sure that our new $capErr variable (array) is acessible via the show function. Go to the beginning of the "Display Record Form" section to do this.

//===============================
// Display Record Form
//-------------------------------
function PositionApply_show()
{
global $db;
global $tpl;
global $sAction;
global $sForm;
global $sYourFormErr; // This is the name of the error variable that we have above
global $capErr; // This is the new variable that we created above

This has to be done otherwise the variables won't be accesible within this function. $sYourFormErr should already be set but you will need to add the "global $capErr;"

Next, we need to create each caption variable and whether it will be red or not. For this, go to the "Show form field" section. Right above the "Show form field" section, we will create the variables there.

Example:
$captionEmail = "Email"; // Regular captions
$captionLetters = "Letters";
$captionNumbers = "Numbers";
$captionDateMonth = "Month";
$captionDateDay = "Day";
$captionDateYear = "Year";

if ($capErr["email"]){
$captionEmail = "<font color=\"red\"><b>Email</b></font>"; // Error captions
}
if ($capErr["letters"]){
$captionLetters = "<font color=\"red\"><b>Letters</b></font>";
}
if ($capErr["numbers"]){
$captionNumbers = "<font color=\"red\"><b>Numbers</b></font>";
}
if ($capErr["date"]){
$captionDateMonth = "<font color=\"red\"><b>Month</b></font>";
$captionDateDay = "<font color=\"red\"><b>Day</b></font>";
$captionDateYear = "<font color=\"red\"><b>Year</b></font>";
}
//-------------------------------
// Show form field
//-------------------------------


Now we will send the caption variables to the template class. This is right under the "Show form field" section. You will want to add these lines just as they appear:
$tpl->set_var("EmailCap", $captionEmail); // The word in the parenthesis will be the name of the template variable inside your HTML - {EmailCap}
$tpl->set_var("LettersCap", $captionLetters);
$tpl->set_var("NumbersCap", $captionNumbers);
$tpl->set_var("MonthCap", $captionDateMonth);
$tpl->set_var("DayCap", $captionDateDay);
$tpl->set_var("YearCap", $captionDateYear);

And at last we add the finishing touch. Open your generated HTML page. Now replace each caption with the new caption template variable. For example, replace the caption that says Email with {EmailCap}.

Well I really hope you do this so that I did not type all this in vein. Let me know if this works for you or if you need anymore help. You can also email me atjjtoubia1@yahoo.com.

Bye!
xnet
Posted: 04/30/2002, 5:23 PM

jjtoubia, you rule!

Thanks a lot!

   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Internet Database

Visually create Web enabled database applications in minutes.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.