Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
pls some one tell that how to validate emailid in stored procedure.
pls some one tell......
Posted

create following function :

SQL
create function IsValidEmail
(
    @email_address varchar(255)
) returns bit
as begin

    declare @ErrMsg varchar(max) = ''

    IF CHARINDEX(' ',LTRIM(RTRIM(@email_address))) > 0
        set @ErrMsg = @ErrMsg + CHAR(13) + 'No Spaces Allowed'

    IF LEFT(LTRIM(@email_address),1) = '@'
        set @ErrMsg = @ErrMsg + CHAR(13) + '@ can not be the first character of an email address'

    IF LEFT(LTRIM(@email_address),1) = '.'
        set @ErrMsg = @ErrMsg + CHAR(13) + '. can not be the first character of an email address'


    if RIGHT(RTRIM(@email_address),1) = '.'
        set @ErrMsg = @ErrMsg + CHAR(13) + '. can not be the last character of an email address'

    if LEN(LTRIM(RTRIM(@email_address ))) - LEN(REPLACE(LTRIM(RTRIM(@email_address)),'@','')) <> 1
        set @ErrMsg = @ErrMsg + CHAR(13) + ' Add @'

    If( CHARINDEX('.',REVERSE(RTRIM(LTRIM(@email_address)))) > 4 )
        set @ErrMsg = @ErrMsg + CHAR(13) + '. Add domain name ex: .com'

    if( (CHARINDEX('.@',@email_address ) > 0 OR CHARINDEX('..',@email_address ) > 0
        OR CHARINDEX('@@',@email_address ) > 0
        OR CHARINDEX('#',@email_address ) > 0
        OR CHARINDEX('^',@email_address ) > 0
        OR CHARINDEX('&',@email_address ) > 0
        OR CHARINDEX('*',@email_address ) > 0
        OR CHARINDEX('(',@email_address ) > 0
        OR CHARINDEX(')',@email_address ) > 0
        OR CHARINDEX('+',@email_address ) > 0
        OR CHARINDEX('=',@email_address ) > 0)
    )
        set @ErrMsg = @ErrMsg + CHAR(13) + 'invalid character'

    declare @Result bit

    if @ErrMsg = ''
        set @Result = 1 --it IS valid
    else
        set @result = 0

    return @Result
end

go


If this function returns 1 then Email id is valid otherwise it is invalid.
 
Share this answer
 
Comments
Maciej Los 14-Feb-13 8:34am    
Wow, clear SQL...
Interesting function, +5!
;)
Bhushan Shah1988 14-Feb-13 8:36am    
Thanks Maciej Los :D
Asim Mahmood 14-Feb-13 8:34am    
Why you need to do it at DB level you RegularExpression at code end and validate that.
[no name] 14-Feb-13 8:48am    
but i need it in stored procedure. pls help me to do validation in stored procedure
Bhushan Shah1988 15-Feb-13 2:40am    
you can use above function in stored procedure.

try dis :

select IsValidEmail(bhushanshah21@gmail.com)

and

select IsValidEmail(bhushan shah21@gmail.com)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900