Simple form validation with WBSP

AID: 1883
  • Status: Published

1450 points

  • ByNurAzije
  • TypeTutorial
  • Posted on2009-10-29 at 05:37:40
Have you ever looked at the database of your registered users? If you do, you will find a lot of garbage data. Emails that does not exist, fake 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 cannot control and validate everything, but at least you will filter some data. It will teach 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>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:

Select allOpen in new window



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>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:

Select allOpen in new window



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

NurAzije is a PHP and WhizBase programmer, who at the time of article publication is working in partnership with WhizBase on several projects.
Asked On
2009-10-29 at 05:37:40ID1883
Tags

Form validation

,

HTML

,

WhizBase

Topic

Scripting Languages

Views
874

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top Scripting Languages Experts

  1. mplungjan

    145,789

    Master

    0 points yesterday

    Profile
    Rank: Savant
  2. Ray_Paseur

    91,292

    Master

    0 points yesterday

    Profile
    Rank: Savant
  3. billprew

    63,376

    Master

    0 points yesterday

    Profile
    Rank: Genius
  4. RobSampson

    54,636

    Master

    0 points yesterday

    Profile
    Rank: Genius
  5. DaveBaldwin

    51,700

    Master

    2,000 points yesterday

    Profile
    Rank: Genius
  6. sedgwick

    43,950

    1,600 points yesterday

    Profile
    Rank: Genius
  7. leakim971

    43,184

    0 points yesterday

    Profile
    Rank: Genius
  8. dgofman

    36,400

    0 points yesterday

    Profile
    Rank: Genius
  9. COBOLdinosaur

    28,424

    0 points yesterday

    Profile
    Rank: Genius
  10. woolmilkporc

    24,200

    0 points yesterday

    Profile
    Rank: Genius
  11. ozo

    20,600

    0 points yesterday

    Profile
    Rank: Savant
  12. Qlemo

    19,984

    2,000 points yesterday

    Profile
    Rank: Genius
  13. chaituu

    19,100

    0 points yesterday

    Profile
    Rank: Sage
  14. tagit

    19,000

    0 points yesterday

    Profile
    Rank: Genius
  15. farzanj

    18,550

    0 points yesterday

    Profile
    Rank: Genius
  16. nap0leon

    15,094

    0 points yesterday

    Profile
    Rank: Sage
  17. paultomasi

    14,700

    0 points yesterday

    Profile
    Rank: Master
  18. StingRaY

    13,136

    0 points yesterday

    Profile
    Rank: Wizard
  19. jason1178

    13,044

    0 points yesterday

    Profile
    Rank: Genius
  20. HainKurt

    12,350

    0 points yesterday

    Profile
    Rank: Genius
  21. HonorGod

    11,600

    0 points yesterday

    Profile
    Rank: Genius
  22. ahoffmann

    11,136

    0 points yesterday

    Profile
    Rank: Genius
  23. basicinstinct

    10,800

    0 points yesterday

    Profile
    Rank: Genius
  24. leew

    10,030

    0 points yesterday

    Profile
    Rank: Savant
  25. ChrisStanyon

    9,864

    0 points yesterday

    Profile
    Rank: Sage

Hall Of Fame