Showing posts with label Regular Expression. Show all posts
Showing posts with label Regular Expression. Show all posts

Sunday, March 21, 2021

How to remove special characters from a string using a function with RegEx

Here is how you can get remove special characters from a string in SQL

-- SELECT dbo.[RemoveCharSpecialSymbolValue] ('naga3fg@#sai') 
CREATE FUNCTION [dbo].[RemoveCharSpecialSymbolValue](@str VARCHAR(500))  
RETURNS VARCHAR(500)  
BEGIN  
DECLARE @startingIndex int  
SET @startingIndex=0  
WHILE 1=1  
	BEGIN  
	SET @startingIndex= patindex('%[^0-9a-zA-Z]%',@str)  
	IF @startingIndex <> 0  
		BEGIN  
			SET @str = replace(@str,substring(@str,@startingIndex,1),'')  
		END  
	ELSE BREAK;  
END  
RETURN @str  
END   

Hope this helps 😀

Saturday, March 20, 2021

How to remove alpha numeric characters from a string using function with RegEx

Here is how you can get alpha numeric characters from a string in SQL

-- SELECT dbo.[RemoveCharSpecialSymbolIntValue] ('naga3@#sai') 
CREATE FUNCTION [dbo].[RemoveCharSpecialSymbolIntValue](@str VARCHAR(500))  
RETURNS VARCHAR(500)  
BEGIN  
DECLARE @startingIndex int  
SET @startingIndex=0  
	WHILE 1=1  
	BEGIN  
		SET @startingIndex= patindex('%[^0-9.]%',@str)  
		IF @startingIndex <> 0  
		BEGIN  
			SET @str = replace(@str,substring(@str,@startingIndex,1),'')  
		END  
		ELSE BREAK;  
	END  
	RETURN @str  
END 

Hope this helps 😀

Sunday, November 13, 2011

Validators Control to allow only Integers

Here are few different ways you can use native .NET validation controls to validate to allow only integers. We can do it in two ways one by using regular expression and one with compare validators

Method 1: Compare Validator
<asp:TextBox ID="NewMobileTextBox" runat="server" Width="280px" autocomplete="off"
CssClass="signuptxtbox" ></asp:TextBox>
<asp:RequiredFieldValidator ID="NewMobileRequired" runat="server" ControlToValidate="NewMobileTextBox"
CssClass="failureNotification" Display="Dynamic" ErrorMessage="New Mobile is required." ToolTip="New Mobile is required." ValidationGroup="ChangeNewMobileValidationGroup">*</asp:RequiredFieldValidator>
<asp:CompareValidator ID="NewMobileCompare" runat="server" ControlToValidate="NewMobileTextBox" CssClass="failureNotification" Display="Dynamic" Operator="DataTypeCheck" ErrorMessage="Mobile Number should be numeric only." ValidationGroup="ChangeNewMobileValidationGroup" Type="Integer">*</asp:CompareValidator>


Method 2: Regular Expression Validator

<asp:TextBox ID="NewMobileTextBox" runat="server" Width="280px" autocomplete="off"
CssClass="signuptxtbox" ></asp:TextBox><asp:RequiredFieldValidator ID="NewMobileRequired" runat="server" ControlToValidate="NewMobileTextBox"
CssClass="failureNotification" Display="Dynamic" ErrorMessage="New Mobile is required." ToolTip="New Mobile is required." ValidationGroup="ChangeNewMobileValidationGroup">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="mobileRGEX" runat="server"
ControlToValidate="NewMobileTextBox" CssClass="failureNotification"
ErrorMessage="Please Enter Only Numbers" ValidationExpression="^\d+$"
ValidationGroup="ChangeNewMobileValidationGroup"></asp:RegularExpressionValidator>

Wednesday, November 02, 2011

Regex validate Email address C#

This is how we can validating email address through C# using Regular Expression. Here is the sample code,

using System.Text.RegularExpressions;



public bool vaildateEmail(string useremail)
{
bool istrue = false;
Regex reNum = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
CaptureCollection cc = reNum.Match(useremail).Captures;
if (cc.Count == 1)
{
istrue = true;
}
return istrue;
}