Technical Article

Alphanumeric and Special Character Validation

,

Inspired in the script submited by ramesh_kondaparthy , this is a set oriented solution for the same problem.


Parameters: 1) @InputString varchar(25)
Accepts the string, which we need to validate

2) @Type int
= 0 validate the string for only alphanumeric characters
i.e Allows characters between [A-Z] ,[a-z] and [0-9]

= 1 validate the string for alphanumeric and special characters - ‘@’ ,’(‘, ‘ )’, ‘_’, ‘-‘

Return type: int

If the passed string is valid, it returns 0
Else 1

Usage:
1) Select dbo.fn_ValidateString('asd(a3)3_3AAAXXX-3',0)
Return 1 – Failure

2) Select dbo.fn_ValidateString('asd(a3)3_3AAAXXX-3',1)
Return 0 – Success


CREATE FUNCTION fn_ValidateStringNew 
(@InputString Varchar(8000), 
 @Type int)
RETURNS int
AS
BEGIN

DECLARE @rint
SET @r = 0

IF @Type = 0
SELECT @r = 1 WHERE @InputString  LIKE '%[^a-z1-9]%'
ELSE
SELECT @r = 1 WHERE @InputString LIKE '%[^a-z1-9@()-_]%'

RETURN @r

END

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating