Search This Blog

Thursday, August 26, 2010

What Regular Expression?A regular expression is a pattern that can match various text strings, used for validations.

Where and when to use Regular Expression?It can be used in the programming languages which supports or has regular expression class as in built or it supports third party regular expression libraries.

Regular expressions can be used to valid different type of data without increase the code with if and case conditions. A number of if conditions can be omitted with single line of regular expression checking.

Benefits of Regular Expression:The following are benefits (not all included) of use of Regular Expression. a) # line of code can be reduced. b) Speed Coding. c) Easy maintenance (you don’t need to change if validation criteria changes, just check the regular expression string). d) Easy to understand (you don’t need to understand the programmer logic on large if statements and case statements).

Elements of Regular Expression:Here are the basic elements of regular expression characters/literals, which can be used to build big regular expressions:

^ ---->literal matches the start of a LINE of input $ ---->literal matches the end of a LINE of input

. ----> Any character (except \n newline)

{...}----> Explicit quantifier notation. [...] ---->Explicit set of characters to match. (...) ---->Logical grouping of part of an expression. * ---->0 or more of previous expression. + ---->1 or more of previous expression. ? ---->0 or 1 of previous expression; also forces minimal matching when an expression might match several strings within a search string. \ ---->Preceding one of the above, it makes it a literal instead of a special character. Preceding a special matching character, see below. \w ----> matches any word character, equivalent to [a-zA-Z0-9] \W ----> matches any non word character, equivalent to [^a-zA-Z0-9]. \s ----> matches any white space character, equivalent to [\f\n\r\v] \S----> matches any non-white space characters, equivalent to [^\f\n\r\v] \d ----> matches any decimal digits, equivalent to [0-9] \D----> matches any non-digit characters, equivalent to [^0-9]

\a ----> Matches a bell (alarm) \u0007. \b ----> Matches a backspace \u0008 if in a [] character class; otherwise, see the note following this table. \t ---->Matches a tab \u0009. \r ---->Matches a carriage return \u000D. \v ---->Matches a vertical tab \u000B. \f ---->Matches a form feed \u000C. \n ---->Matches a new line \u000A. \e ---->Matches an escape \u001B

$number ----> Substitutes the last substring matched by group number number (decimal). ${name} ----> Substitutes the last substring matched by a (? ) group. $$ ----> Substitutes a single "$" literal. $& ----> Substitutes a copy of the entire match itself. $` ----> Substitutes all the text of the input string before the match. $' ----> Substitutes all the text of the input string after the match. $+ ----> Substitutes the last group captured. $_ ----> Substitutes the entire input string.

(?(expression)yes|no) ----> Matches yes part if expression matches and no part will be ommited.

Simple Example:Let us start with small example, taking integer values: When we are talking about integer, it always has fixed series, i.e. 0 to 9 and we will use the same to write this regular expression in steps.

a) Regular expression starts with “^” b) As we are using set of characters to be validated, we can use []. c) So the expression will become “^[1234567890]” d) As the series is continues we can go for “-“ which gives us to reduce the length of the expression. It becomes “^[0-9]” e) This will work only for one digit and to make it to work for n number of digits, we can use “*”, now expression becomes “^[0-9]*” f) As with the starting ending of the expression should be done with “$”, so the final expression becomes “^[0-9]*$”

Note: Double quotes are not part of expression; I used it just to differentiate between the sentences.

Is this the way you need to write:This is one of the way you can write regular expression and depending on the requirements and personal expertise, regular expression could be compressed much shorter, for example above regular expression could be reduced as.

a) Regular expression starts with “^” b) As we are checking for the digits, there is a special character to check for digits “\d” c) And digits can follow digits , we use “*” d) As expression ends with “$”, the final regular expression will become "^\d*$”

Digits can be validated with different ways of regular expressions:

1) ^[1234567890]*$ 2) ^[0-9]*$ 3) ^\d*$

Which one to choose?Every one of above expressions will work in the same way, choose the way you are comfort, it is always recommended to have a smaller and self expressive and understandable, as these will effect when you write big regular expression.

Example on exclude options:There are many situation which demands us to exclude only certain portion or certain characters,Eg: a) Take all alpha numeric and special symbols except “&&#8221; b) Take all digits except “7” then we cannot prepare a big list which includes all instead we use the symbol of all and exclude the characters / symbols which need to be validated. Eg: “^\w[^&]*$” is the solution to take all alpha numeric and special symbols except “&&#8221;.

Other Examples:a) There should not be “1” as first digit,? ^[^1]\d*$ ? this will exclude 1 as first digit.

b) There should not be “1” at any place? ^\d[^1]*$ ? this will exclude the 1 at any place in the sequence.

Note: Here ^ operator is used not only to start the string but also used to negate the values.

Testing of Regular expression:There are several ways of testing this a) You can write a windows based program. b) You can write a web based application. c) You can even write a service based application.

Windows base sample code:Here are steps which will be used for regular expression checking in dotNet:

a) Use System.Text.RegularExpression.Regex to include the Regex class. b) Create an Regex object as follows: Regex regDollar= new System.Text.RegularExpressions.Regex("^[0-9]*$ "); c) Call the IsMatch(string object) of the Regex call, which will return true or flase. d) Depending on the return state you can decide whether passed string is valid for regular expression or not.]

Here is the snap shot code as function:Public boolean IsValid(string regexpObj, string passedString) { //This method is direct method without any exceptional throwing.. Regex regDollar= new System.Text.RegularExpressions.Regex(regexpObj); return regDollar.IsMatch(passedString); }With minor changes to the above function it can be used in windows or webbased or even as a service.

Another way -- Online checking: At last if you are fed up with above and you have internet connection and you don’t have time to write sample, use the following link to test online http://www.regexplib.com/RETester.aspx

There is no doubt that Joomla! is one of the best content management system (CMS) in the world. It has a large set of extensions to meet almost any type of application need ranging from content management, photo gallery, multimedia streaming to e-commerce and social networking. It has great flexibility in changing designs and customizing the code. With Joomla, you can build any kind of website and dynamic web application. For example, this step-by-step tutorial by Suhreed Sarcar describes how to build custom form applications in Joomla! using CK Forms component without delving into any PHP code.

Add a quick survey form to your Joomla site

Dynamic web application often means database at the back-end. It not only takes data from the database and shows, but also collects data from the visitors. For example, you want to add a quick survey into your Joomla! Site. Instead of searching for different extensions for survey forms, you can quickly build one survey form using an extension named CK Forms. This extensions is freely available for download athttp://ckforms.cookex.eu/download/download.php.

For creating a new form, click on New icon in the toolbar. That opens up CK Forms: [New]screen.

In the General tab, type the name and title of the form. The name should be without space, but title can be fairly long and with spaces. Then select Yes in Published field if you want to show the form now. In Description field, type a brief description of the form which will be displayed before the form.

In Results tab, select Yes in Save result field. This will store the data submitted by the form into database and allow you to see the data later. In Text result field, type the text that you want to display just after submission of the form. In Redirect URL field, type the URL of a page where the user will be redirected after submitting the form.

In E-mail tab, select Yes in Email result field, if you want the results of the forms to be e-mailed to you. If you select Yes in this field, provide mail-to, mail cc, and Mail BCC address. Also specify subject of the mail in Mail subject field. Select Yes in Include fileupload file field if you want the uploaded file to be mailed too. If an e-mail field is present in the form and the visitor submits his/her e-mail address, you can send an acknowledgement on receiving his/her data through e-mail. For sending such receipt messages, select Yes in E-mail receipt field. Then specify e-mail receipt subject and e-mail receipt text. You can also include the data and file submitted via the form with this receipt e-mail. For doing so, select Yes in both Include data and Include fileupload file fields.

In Advanced tab, you can specify whether you want to use Captcha or not. Select Yes in Use Captcha field. Then type some tips text in Captcha tips text, for example, 'Please type the text shown in the image'. You can also specify error texts in Captcha custom error text field. InUploaded files path field, you can specify where the files uploaded by the users will be stored. The directory mentioned here must be writable. You can also specify the maximum size of uploaded file in File uploaded maximum size field. To display Powered by CK Forms text below the form, select Yes in Display "Powered by" text field.

In Front-end display tab, you can choose to display IP address of the visitor. You can view help on working with CK Forms by clicking on Help tab.

After filling up all the fields, click on Save icon in the toolbar. That will save the form and take you to CK Forms screen. Now you will see the newly created form listed in this screen.

Adding fields to the form

Now it's time to add fields to the form. For adding the fields to a form, follow the steps below:

For doing so, click on pencil icon in the Fields column against the form name. That opens upCK Forms – Fields screen.

For adding a new field to the form, click on New button in the toolbar. That opens up CK Forms – Fields: [New] screen.

In General tab, type the name of the field. The name can be alpha-numeric without any space. However, Label field can contain spaces and will be displayed on the form. In Published field, select Yes to display the field on the form. Then select a field type from Type drop-down list. You can select one of the following types from this list.

Type

Description

Text

This creates a text field on the form where single line texts can be typed by the users. You can specify length of the field. This creates <input type=text ... /> HTML markup.

Text field types in CK Forms can be any of the following sub-types:

Text – this is plain text field where users can type texts. For example, user's name or title of an article.

E-mail – this sub-type is used to validate e-mail addresses provided by the users.

Password – this type of field is used for typing passwords. The texts typed in these fields are masked with *** so that others in front of the screen cannot see typed passwords.

Date – selecting this sub-type will display a date selector in that field. You can also specify date formats allowed in this field.

Number – This sub-type allow only inputting numbers in the field.

Hidden

This creates a hidden field where some information can be embedded with the form. It is equivalent to <input type=hidden ... /> markup.

Textarea

This creates a field where users can type multiple lines of text. This is equivalent to <textarea></textarea> markup.

Checkbox

Checkboxes are for selecting one or more options at a time. This is equivalent to <input type="checkbox" name="..." value="..." />markup.

Radio button

Radio button are for presenting options those are mutually exclusive. For example, sex of a person can either be Male or Female. In that case, you should use radio buttons. This is equivalent to <input type="radio" name="..." value="..." /> markup.

Select

This type of fields creates drop down list or select list from where users can select one or more than one options. This is similar to following code block:

This creates a form control that allows uploading a file to web server. It is equivalent to <input type=”file” name=”file1” /> markup.

Button

This creates buttons, such as Submit or Reset. This is equivalent to<input type=”button” name=”...” /> markup.

Field separator

This creates a separator between two fields. It is simply a space or horizontal line.

Let us start a field for typing Name of the visitor. After clicking on New button, in General tab, type 'name' in Name and 'Name' in Title field. Then select Text from Type drop-down list. That changes the list of fields below this. Now fill the fields as shown in the following screen shot. Please remember to select 'Text' in Text type drop down list. Then click on Save button in the toolbar.

Create another field, name it as 'dob' and title as 'Date of Birth'. From Type drop down list, selectText, and in Text type drop down list select Date. The configuration for this field will look like the following screen shot. When done entering these information, click Save button in the toolbar to save the changes.

Now add a field with name 'sex' and title 'Sex'. Select Radio Button from Type drop down list. InDisplay field, select 'In line'. In Value and Label fields, type value and labels for options to be displayed and click on Add button. The value and label pairs will be listed in Checkbox list field. When done adding options, click Add button in the toolbar.

Add a maritalstatus field of type Radio button. Then create a field name email of type Text. Select Text type as E-mail. The configuration for the field will look like the following screen shot.

Now add a new field, name it as country and give a title 'Country'. Select 'Select' from Type drop down list. In Value and Label fields types value and labels of the options you want to display, and click Add button to add the value-label pairs in Checkbox list field. You can delete any value-pair list from this box by selecting the pair and clicking del button. When finished adding options, click on Add button in the toolbar.

Now add another field. Name it as subscribe, and type 'Do you want to subscribe our newsletter?' in Title field. Select 'Checkbox' from Type drop down list. In Value field, type 'subscribe'. For making this checked default, check Checked field. When done, click on Addbutton in the toolbar.

Now we will be adding a textarea field. Name the field as 'resume' and Title as 'Describe yourself briefly'. From the Type drop down list, select Textarea. For allowing use of HTML text and visual editor in this text area, check HTML Editor checkbox. In Wrap drop down list, selectVirtual. Save the field by clicking on Save button in the toolbar.

Now we will be adding a field through which users will be able to upload their curriculum vitae in Microsoft Word (.doc), PDF (.pdf) or OpenOffice Document (.odt) format. Add a new field, name it cvitae and title as 'Upload your curriculum vitae'. Select File upload in Type drop down list. Save the filed by clicking on Save button in the toolbar.

Now it's time to create some action for the form. Add a new field by naming it 'submit' with title 'Submit'. Then select Button from Type drop down list. Selecting Button in this field shows another Type drop down list just below it. Select Submit from this drop down list. Click on Savebutton in the toolbar.

Similarly create a Reset button by selecting Button and then Reset in Type drop down list.

Now we have finished adding the buttons to the form. It's time to add this form in front-end menu so that users can click on that, fill in and submit the form.

Adding menu item for the form

For adding a menu item linking to the form we have created, follow the steps below:

Click on Menus | Main Menu. That shows Menu Item Manager: [mainmenu] screen. Click onNew button in the toolbar on this screen. That shows Menu Item:[new] screen. Click on CK Forms and you get sub-items under it, as shown in the following screen shot.

Click on Standard CKForms CSS Layout link. That shows Menu Item:[new] screen, on which you have to type the name of the menu, its alias and some other information as shown in the following screen shot.

In Title field type the title of the menu, and its short name in Alias field. On the right side of this screen, select the form name you have created (for our case it is 'quicksurvey') in Select CK forms drop down list. When done, click on Save button in the toolbar. Now the menu is added to main menu.

Previewing the Form

From the administration panel, click on Preview button on the upper right-hand side. That previews your site. You see Quick Survey menu item in Main Menu. Click on this menu item and you see your newly created form Quick Survey. It looks like the following screen shot.

As you see, the date of birth field shows a calender icon besides it, taking your mouse pointer to this field you display a date selector. Sex and Marital Status fields are showing radio buttons. For the first, options are displayed in line, whereas for Marital Status, options are shown as list. E-mail address field looks like a simple text field, but when you type something in this field and move away from the field, the contents will be instantly validated. It will validate whether you have given an e-mail address or not, and show error message.

Country field displays a drop down list with the names of countries we have added. Similarly, you also see a newsletter subscription checkbox. The textarea field we created is displayed with HTML Editor. If you like you can turn off this Visual HTML editor by clicking on Toggle editor button. Then we see, file upload field. Clicking on Browse button opens up file opening dialog from where users can select a file for uploading to Joomla! site.

During form definition we have indicated that we want Captcha. You see the Captcha image and a box to type the image text. You can also refresh the Captcha code.

Viewing data submitted by the form

As an administrator of a Joomla! Site you can easily see the data submitted through the form. For viewing the data submitted follow the steps below:

For viewing data submitted by a form, click on Display Data link on the right side on the form name (in Data column). That opens up CK Forms – Data screen. This screen lists the data submitted by the form.

You can view these data and export as CSV file. For exporting as CSV file, select data sets and click on Export button. You can also delete the data by selecting data sets and clicking onDelete button.

For showing data in the front-end, follow the steps below:

Select Menus | Main Menu. That shows Menu Item Manager:[mainmenu] screen. Click on Newbutton on this screen. That shows Menu Item:[new] screen. On this screen, click on CK Formslink, and you see sub-items under it. From the sub-items of CK Forms, click on Standard data CKForms CSS Layout link. That opens up Menu Item:[new] screen.

In Menu Item:[new] screen, type the title of menu link, and from Parameters (Basic) section, select the form from Select data display profile drop down list. You can also define whether you want to show column headers and table borders or not. When done, click on Save button in the toolbar.

Preview the site and click on the menu item created. You will see the data submitted by the form as shown in the following screen.

Users can now see all submitted data as a table. At this moment, you cannot selectively publish submitted data.

More things to do with CK Forms

With CK Forms component, you can create simple form applications that do not need multiple tables and complex relationships. As seen in this tutorial, you can easily add one page simple form on your Joomla! site without any coding. Besides the features covered in this tutorial, you can also do the following with CK Forms:

You can duplicate an existing Form alongwith its associated fields.

You can backup a form with its data as .sql file. When needed, you can also restore the form definition and data associated with it.

Form's data can be exported as CSV file that can e used for other applications.

For each field, you can define the layout by editing CSS in the Layout tab.

For overall layout of forms created by CK Forms, you can edit the CK Form CSS from within the CK Form control panel.

Summary

There are many extensions available for Joomla! through which functionalities of a Joomla! Site can be enhanced. For building form applications, there are several extensions and CK Form is one of them for building simple forms in Joomla!. Using CK Forms extension you can easily add a custom form into Joomla! By which users can submit data to you. For doing this, you don't need any coding except using some wizards in the component.

3. copy this code , paste this into your websites code , just before the </head> tag

(you can put it anywhere you like, but i think it's more manageable).

ex. if you want to tract your whole site , you need to add this script to every page. As your website can be accessed using search engine ( directly to a pin point page, adding it to a index page may not be helpful)

If you are a blogger, just add that to the index page and that's enough.

4. That's it.. all done.

now it may take up to 24 hrs for the website to start up and show you the history.

Google Analytics will itself check to see that the tracking code has been installed correctly on the home page of your site - once you've created a new profile, the Tracking Status will display a warning icon, "Tracking status unknown," until the system detects the code. You can see what the different status icons look like and mean

After you install the Google Analytics Tracking Code on your site, you will not see data appearing in your reports until at least 24 hours. You can always check the status of your installation in the Website Profile summary box, under the column "Status." The following tracking status messages can appear for your profile:

Waiting for Data

The tracking code has been detected on the home page of your website, and Google Analytics is aggregating the data to populate into your reports.