Jul 2, 2009

Simple form validation with WBSP

Have you ever looked to the DB of your registered users, if you do you will find a lot of garbage data. Emails that does not exist, unreal names and comments with spam or empty fields.



In this tutorial I will show you how to make a simple validation form using WhizBase. With it you can not control and validate everything, but at least you will filter some data. It will learn you some basics and it will save you a lot of deleting work.



The boring part


The most boring part for a developer is HTML, generally programmers and developers do not like designing. If you ask me, I hate HTML coding, I mostly give it to my wife, she likes it and I work on the more complicated things «Server Side Scripting».

Let us make a file named as «form.htm» and put in it the next code:




<html>

<head>

<title>Feedback Form</title>

</head>

<body>

<form action="validation.wbsp" method="post">

<fieldset><legend>Enter your information in the form below.</legend><br />

First Name:<br/>

<input type="text" name="fname" size="20" maxlength="40" /><br /><br />

Last Name:<br/>

<input type="text" name="lname" size="40" maxlength="60"/><br /><br />

E-Mail:<br />

<input type="text" name="email" size="40" maxlength="60"/><br /><br />

Comments:<br />

<textarea name="comments" cols="40" rows="7"></textarea>

</fieldset>

<div align="center"><input type="reset" />

<input type="submit" name="submit" value="Submit" /></div>

</form>

</body>

</html>



Pay attention to the action attribute in the form tag, we will send our data to validation.wbsp which is our WhizBase Server Pages file.



The Need For Speed Part


Have you ever played Need For Speed, I like the Lamborghini car, that is a great game. WhizBase is something like that to other languages when you are developing a site. You do not need a lot of time to create it.



Let us create the file validation.wbsp and put WhizBase code in it:




[FormFields]
WB_Required=email,fname,lname,comments

<!--WB_BeginTemplate-->

<html>

<head>

<title>Validate Form</title>

</head>

<body>

$wbif[$wbcstr[$wbv[email]|@]>1|Please insert your email address<br />|]

$wbsplit[$wbv[email]|email_array|@]

$wbif[$wblen[$wbgetv[email_array(0)]]>0||Your have inserted an invalid email format<br />]

$wbif[$wblen[$wbgetv[email_array(1)]]>0||Your have inserted an invalid email format<br />]

$wbsplit[$wbgetv[email_array(1)]|domain|.]

$wbif[$wblen[$wbgetvdomain(0)]]>0||Your have inserted an invalid email format<br />]

$wbif[($wblen[$wbgetvdomain(1)]]>1) and ($wblen[$wbgetvdomain(1)]]<5)||Your have inserted an invalid email format<br />]

</body>

</html>



We need to check 4 values (email,fname,lname,comments) if they are empty or not, then in case if they are empty we will give an error. We simply use one command, it is WB_Required which will check all the fields required, if one of them are empty it gives an error.



[FormFields] is a command where we tell the server to start processing essential data for WhizBase file, and we close it with <!--WB_BeginTemplate--> command.



When we come to email validation we need to validate its format also, it is important if it is in the format of:

Charactars_and_Numbers@ Charactars_and_Numbers.4_to_2_Charactars_and_Numbers



From this we need to check:


  • does it have @ symbol, and only once.

  • Before @ symbol we need to have at least 1 character

  • Do we have a dot in the second part which we split with it the rest to two parts.

  • The first part must be at least one character

  • The second part must have at least 2 characters and at most 4




Let us start playing cards.

First we need to check if there is and only one @ symbol in the email, if it is false we give an error. This is done with if statement and wbcst function.



$wbif[$wbcstr[$wbv[email]|@]=1|| Please insert your email address<br />]


The IF:

$wbif[ CONDITION | IF TRUE | IF FALSE]




The CONDITION:

$wbcstr[ STRING | STRING TO LOOK FOR] = 1



In WhizBase get and post methods are considered the same, if you send a variable using post method or send it using get method all the variables are created on the fly as a special set.

Considering that we will just get these variables with $wbv[var_name].



The STRING:
$wbv[email]



Second we split the email address to two parts and put it in an array, then we check the first part if it is at least 1 character.



$wbsplit[$wbv[email]|email_array|@]

$wbif[$wblen[$wbgetv[email_array(0)]]>0||Your have inserted an invalid email format<br />]



We notice WBSPLIT function, it takes a string and an array name and a separator, creates the array and fill the string's part separated by the separator.



$wbsplit[ STRING | ARRAY_NAME | SEPARATOR]



In the second line we get the length of the first part by using WBLEN and check if it is more than zero characters.



$wblen[ STRING ]

To access a variable we need to use the WBGETV function which accesses variables and arrays, and we call the first part of the array with (0) - in WBSP 0 is the first element in the array.



The STRING:

$wbgetv[ VARIABLE_NAME ]



Third we make the same process to the other part with the dot, and we have a simple email validation done.



For more information email me at:
NurAzije [at] Gmail [dot] com

Or visit WhizBase official site at www.whizbase.com

No comments: