Showing posts with label Regex. Show all posts
Showing posts with label Regex. Show all posts

Wednesday, 2 July 2014

It is very easy to remove string's leading and trailing white spaces using Regex.

 The Regex is "^\s+|\s+$".

Here is example of this.

In this example we are taking on string which has leading and trailing white spaces. We can remove this spaces using Regex.

C#. Net Example :
string strData = "            This string contains leading and trailing white spaces             ";
strData = Regex.Replace(strData, @"^\s+|\s+$", "");
Response.Write(strData);

VB.Net Examples :
Dim strData As String = "            This string contains leading and trailing white spaces             "
strData = Regex.Replace(strData, "^\s+|\s+$", "")
Response.Write(strData)

  
Click here for other post regarding Remove only leading white spaces from string using Regex with C#.Net and VB.Net example


Click here for other post regarding Remove only trailing white spaces from string using Regex with C#.Net and VB.Net example


Wednesday, 25 June 2014

It is very easy to remove string's trailing white space.

We can achieve using Regex. The Regex is "[ \t]+$".

Here is example of this.

In this example we are taking on string which has trailing white space. We can remove this spaces using Regex.

C#. Net Example :
string strData = "This string contains trailing white spaces             ";
strData = Regex.Replace(strData, "[ \t]+$", "");
Response.Write(strData);

VB.Net Examples :
Dim strData As String = "This string contains trailing white spaces             "
strData = Regex.Replace(strData, "[ \t]+$", "")
Response.Write(strData)


Click here for other post regarding Remove only leading white spaces from string using Regex with C#.Net and VB.Net example

Click here for other post regarding Remove leading and trailing white spaces from string using Regex with C#.Net and VB.Net example.  


Wednesday, 18 June 2014

It is very easy to remove string's leading white space.

We can achieve using Regex. The Regex is "^[ \t]+".

Here is example of this.
In this example we are taking on string which has leading white space. We can remove this spaces using Regex.

C#. Net Example :
string strData = "   This string contains leading white spaces";
strData=Regex.Replace(strData, "^[ \t]+", "");
Response.Write(strData);

VB.Net Examples :
Dim strData As String = "   This string contains leading white spaces"
strData = Regex.Replace(strData, "^[ \t]+", "")
Response.Write(strData)


Click here for other post regarding Remove only trailing white spaces from string using Regex with C#.Net and VB.Net example

Click here for other post regarding Remove leading and trailing white spaces from string using Regex with C#.Net and VB.Net example.  
 

Friday, 20 December 2013

You can validate the data of XML file and identify XML elements which are failed to validate. We can easily achieve this using LINQ.

Here is example for this.
In this example we take one 'customer.xml' file. In this file we have 'name' and 'phone' element. We need to validate phone number with REGEX and display it's validate result either it is true or false.

Sunday, 18 November 2012

There is a situation in drop down list where first item is "[Select]" and you need to make mandatory selection from drop down list except "[Select]".
Means, user must select value from drop down list but did not allow to select first element "[Select]". We can achieve this using required field validator control. For that we need to set "InitialValue" property.

Here is example for this.
In this example we take one product drop down list control in this list we list out all products, But the First Item is "[Select]" and it's value is "-1" in product drop downlist.
And We take one Required Field Validator control and set it's ValidateControl property to product drop down list id. We also set "InitialValue=-1" property.
We also took one button so that on click of button required field valdator control validate user input.
In this example we set InitialValue to -1 so that , if user select "[Select]" item from drop down list and click on "Save" button Validator control executed and validation message display on screen.
You can see that validation message in output.

Monday, 8 October 2012

We often get requirement to allow only numeric decimal value in textbox for validation purpose, And also we have to specify number of decimal places. We can use ASP.Net RegularExpressionValidator control for this. In this control we can specify regex expression for validation.

The regex expression is "^[+]?[0-9]\d{0,5}(\.\d{1,4})?%?$" .
This regex allow only positive numeric value of maximum six digit and maximum four digit decimal value. The maximum allowed value by this regex is "999999.9999". You can change this accroding to your requirement.

Here is example.

Thursday, 4 October 2012

For validation purpose in your project at some places you put text boxes that accept only integer value i.e. 1, 20 , 50, 200 etc... To put validation on that text boxes and allow only integer value you can use ASP.NET Regular Expression Validator Control. In this control we make one regex expression and do validation for that text box.
The regex expression is "^\d+$" .

Here is example.

Thursday, 23 August 2012

 Click Here to Download TextBoxMaximumLengthApplication.zip

There is always a need of application to set maximum length allowed in textbox.
In this article we demonstrate both single line textbox and multi line textbox max length validation. 
There is a different way to set maximum characters allowed for single line textbox and multiline textbox.
You can set "MaxLength" property for single line textbox.

 For multiline textbox "MaxLength" does not supported.

So for Multi Line textbox we need some tricky approch. We can use javascript , JQuery or server side checking. Here we are going to use .Net Validator controls. We are using "RegularExpressionValidator" control.

Here is example for this.

In this example we are taking two textbox one is for ProdutTitle which is singleline textbox and other is for ProductDescription which is MultiLine textbox. For ProductTitle we are using MaxLength property to set maximum allowed characters. Which is 20 characters.
For ProductDescription we are using "RegularExpressionValidator" control to set maximum allowed characters. Which is 50 characters.
We are using Regex expression for validation.
The "RegularExpressionValidator" control display error message after typing in textbox focus or cursor moved out from textbox.
You can also make server side validation check for regular expression validation using "Page.Validate()" Method and "Page.IsValid" property.

In output you can see that if more than maximum allowed characters type in textbox it will give error message. You can also make you customize message.


ASPX Code:
        <table>
            <tr>
                <td>
                    Product Title :
                </td>
                <td>
                    <asp:TextBox runat="server" ID="txtProductTitle" MaxLength="20" ></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Product Description :
                </td>
                <td>
                    <asp:TextBox runat="server" ID="txtProductDescription"  TextMode="MultiLine"></asp:TextBox>
                    <asp:RegularExpressionValidator runat="server" ID="REVProductDescription" ControlToValidate="txtProductDescription"
                        ValidationExpression="^[\s\S]{0,50}$" style="color:Red;font-weight:bold" ErrorMessage="Please enter a maximum of 50 characters"
                        Display="Dynamic"></asp:RegularExpressionValidator>
                </td>
            </tr>
        </table>

Output : 
(To view original image , click on image)

For Beginning .Net articles. Click Here...
 
Here is very useful .Net Tips.


Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.