2016-11-03 96 views
0

在mysql中驗證電子郵件地址我已經通過有效的電子郵件地址,在MySQL存儲過程中沒有得到一個問題,這裏就是想發生:在存儲過程

DELIMITER go 
Create procedure AddMessages(
    Out MessageID tinyint(11), 
IN iFirstName varchar(30), 
IN iLastName varchar(30), 
IN iTextBox varchar(400), 
IN iEmailAddress varchar(30)) 
BEGIN 
/*declaring local variable*/ 

    If(iFirstName='') then 
    SIGNAL SQLSTATE '45000' 
    SET MESSAGE_TEXT = 'Fill out the First Name'; 
    Elseif(iLastName='') then 
    SIGNAL SQLSTATE '45000' 
    SET MESSAGE_TEXT = 'Fill out the Last Name'; 
    Elseif(iTextBox='') then 
    SIGNAL SQLSTATE '45000' 
    SET MESSAGE_TEXT = 'Fill out the Message Box'; 
    Elseif(iEmailAddress='') then 
    SIGNAL SQLSTATE '45000' 
/* Checking if email address is not valid*/ 
    SET MESSAGE_TEXT = 'Fill out the Email Address'; 
    Elseif(iEmailaddress!='^[a-zA-Z0-9][a-zA-Z0-9._-]*@[a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a‌​-zA-Z]{2,4}$') then 
    SIGNAL SQLSTATE '45000' 
    SET MESSAGE_TEXT = 'Please enter the correct email address' 
; 
Else 
/* insert the messages*/ 
    insert into Messages( 
    FirstName, 
    LastName, 
     Messagetextbox, 
    EmailAddress 
    ) 
    Values 
    (
    iFirstName, 
    iLastName , 
    iTextBox , 
    iEmailAddress 
    ); 
    set MessageID = last_insert_id(); 
    end if; 
    End 

當我用

測試它
set @new_id = null; 
call AddMessages(@new_id,'Jamie','Jones','Hello','[email protected]'); 
select @new_id; 

我是,我一直得到消息,說請 輸入正確的郵箱地址,我已經測試了不使用 @和它的作品,但由於某種原因,驗證電子郵件蘇結果ppose不通過 顯示相同的消息。

+0

您不能使用'!='進行正則表達式匹配。使用'IF iEmailaddress NOT RLIKE'...'' – Barmar

+0

'='和'!='做精確的字符串匹配,而不是正則表達式。 – Barmar

+0

請加**,請**允許超過30個字符的電子郵件地址。由於「david.jones @ corporationlatetotheparty.com」不是一個不尋常的地址,你會看到很多人碰到這個限制。使用'VARCHAR(255)'作爲任何通用文本字段,除非你有一個非常有說服力的理由來改變默認值。 – tadman

回答

1

它更改爲:

Elseif (iEmailaddress NOT RLIKE '^[a-zA-Z0-9][a-zA-Z0-9._-]*@[a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a‌​-zA-Z]{2,4}$') then 

RLIKE是用於匹配正則表達式的運算符。 =!=完全匹配字符串。

+0

技術上正確,但哇,是來自問題ever * bad *的正則表達式。 – tadman

+0

@tadman我甚至懶得去檢查正則表達式,我覺得這足以幫助他使用SQL語法。 – Barmar