Welcome! Log In Create A New Profile

Advanced

How to use returntocapture after validating a form

Posted by Piet van Zanten 
Piet van Zanten
How to use returntocapture after validating a form
November 12, 2011 01:02PM
Hi,

I have developped a rather elegant form validation procedure. (if I may say so myself :xcoolsmiling smiley
First, define two global variables in the project initialization code: sControl and sError as string.

Then create a server procedure like:

PROCEDURE Check_Input()
IF NOT CaptchaVerify(CPTCH_Captcha,edt_Captcha) THEN
sError="The captcha was not entered correctly."
sControl=edt_Captcha..Alias
RESULT False
END
IF edt_UserName="" THEN
sError="Please enter a username."
sControl=edt_UserName..Alias
RESULT False
END
IF HReadSeekFirst(Users,Name,edt_UserName) THEN
sError="This username is already used by someone else."
sControl=edt_UserName..Alias
RESULT False
END
//put some more checks here if you like
sControl=""
sError=""
RESULT True

In the form submit button click code (server) you enter:

IF NOT Check_Input() THEN
PageRefresh(PAGE_Form)
RETURN
END

Finally you put this code in the Load (onload) code of the page:

IF sError<>"" THEN
Error(sError)
ReturnToCapture(sControl)
END

Have fun.
Regards,
Piet

edit: using ..Alias instead of ..Name really simplifies the on load code!



Edited 1 time(s). Last edit at 05/01/2012 12:25PM by Piet van Zanten.
Piet van Zanten
Re: How to use returntocapture after validating a form
September 23, 2012 04:24PM
My validation procedure has evolved:

The global server procedure is called with a list of parameters separated by CR.
Each parameter has an optional subparameter attached to it separated by a tab.
The subparameter indicates special filetypes like email, date etc.
In the global declarations code of the page we declare two string variables:
sError,sControl is string
The server procedure is called in the form validation button click code (server)
x is string

x=Check_Input(edt_Roepnaam..Name+RC+edt_Voorl..Name+RC+edt_Naam..Name+RC+edt_GebDat..Name+TAB+"date"+RC+edt_Email..Name+TAB+"email")
IF x<>"" THEN
	sError=ExtractString(x,1)
	sControl=ExtractString(x,2)
	RETURN
        //code continued, validation o.k.
END
The server procedure:
PROCEDURE Check_Input(sFieldList)
x,sError,sControl,sStart is string

FOR EACH STRING sField OF sFieldList SEPARATED BY CR
	x=ExtractString(sField,1)
	sStart="The field '"+{x,indControl}..Caption+"' "
	SWITCH ExtractString(sField,2)
		CASE "email" 
			IF NOT CheckEmail({x,indControl}) THEN
				sError=sStart+"does not contain a valid email addres"
			END
		CASE "date" 
			IF NOT DateValid({x,indControl}) THEN
				sError=sStart+"does not contain a valid date."
			END
		CASE "bank" 
                        //this is a typical Dutch check for bank account numbers
			IF NOT Elfproef_OK(0,{x,indControl},True,9) THEN
				sError=sStart+"voldoet niet aan de 11-proef."
			END
		OTHER CASE
			IF {x,indControl}="" THEN
				sError=sStart+" is required."
			END
	END
	IF sError<>"" THEN
		sControl={x,indControl}..Alias  //By using the alias we can use the sControlname in browser code
		BREAK
	END
END
IF sError="" THEN
	RESULT ""
ELSE
	RESULT sError+TAB+sControl
END

Finally in the Load (onload) code of the page:
IF sError<>"" THEN
	Info(sError)
	ReturnToCapture(sControl)
END
To do:
Probably better to turn this into a class, so it can be loaded and unloaded when necessary.
I assume a global procedure resides in memory all the time.
Re: How to use returntocapture after validating a form
November 14, 2018 01:24PM
Thanks
Renko vd molen
Re: How to use returntocapture after validating a form
July 07, 2021 10:59AM
Form validation ++. if filed in etc shoul dbe done client side . On server if you want to check data validity
This is done verry easy in webdev.. Creating a claas for this ?? big big overhead on something as easy as validation . Just my thoughts
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: