2013-09-26 16 views
0

我在我的模型中的兩個驗證abc.rbvalidates_format_of:用=> URI ::正則表達式(%W(HTTP HTTPS))問題

validates_format_of :url, :with => URI::regexp(%w(http https)) 
validates_format_of :targetUrl, :with => URI::regexp(%w(http https)) 

validates_format_of :targetUrl 

不驗證輸入。無論我輸入它接受。同樣的問題,我在其他模型,我有驗證 validates_format_of :website, :with => URI::regexp(%w(http https))。 我無法弄清爲什麼驗證失敗的原因爲
validates_format_of :targetUrl, :with => URI::regexp(%w(http https))validates_format_of :website, :with => URI::regexp(%w(http https))

任何提示都將非常有幫助。 謝謝

回答

0

你不需要以這種方式指定正則表達式格式。使用類似的東西:

validates :website, format: { with: /(http|https):\/\/[a-zA-Z0-9\-\#\/\_]+[\.][a-zA-Z0-9\-\.\#\/\_]+/i } 

顯然使用任何你喜歡的正則表達式!

+0

同樣的問題。我很好奇爲什麼同樣的驗證不適用於其他領域。 – user588324

+0

同樣的問題是什麼?這是我在我的一個應用程序中使用的驗證,所以它肯定有效... – tyler

+0

它未驗證網址。我輸入它接受的垃圾。另外我不尋找新的驗證。我想知道爲什麼相同的驗證格式適用於一個字段而不適用於其他字段。 – user588324