2012-04-28 160 views
118

例如我有一個textfield。本場是強制性的,只有數字是必需的,有價值的長度必須是10。當我嘗試提交表單與值長度是5,出現默認的錯誤消息:Please match the requested format如何更改或刪除HTML5表單驗證的默認錯誤消息?

<input type="text" required="" pattern="[0-9]{10}" value=""> 
  1. 我怎樣才能改變HTML 5表單驗證錯誤默認消息?
  2. 如果第一點可以完成,那麼有沒有辦法創建一些屬性文件並在該文件中設置自定義錯誤消息?
+1

我發現Mahoor13答案錯誤,它不是在工作循環,所以我已經修復了它並給出了一些更正的答案,你可以在答案部分找到它。 **這是鏈接** http://stackoverflow.com/questions/10361460/how-can-i-change-or-remove-html5-form-validation-default-error-messages/42430173#42430173 – 2017-03-15 08:31:53

回答

181

我發現ANKUR答案錯誤,我已經用這種修正固定它:

<input type="text" pattern="[a-zA-Z]+" 
    oninvalid="setCustomValidity('Plz enter on Alphabets ')" 
    onchange="try{setCustomValidity('')}catch(e){}" /> 

當您設置了無效的輸入數據中看到的錯誤,然後校正輸入併發送形式。 哎呀!你不能這樣做。 我已經測試了Firefox和Chrome

+15

我建議不要使用內聯事件。這不是一個好習慣。 – Jared 2013-05-01 06:54:59

+2

@ Mahoor13我寫了類似的,但它永遠不會驗證。你能給我一個提示嗎? http://jsfiddle.net/2USxy/ – Sliq 2013-05-31 23:36:22

+0

html5表單驗證需要現代瀏覽器,請參閱此頁: http://diveintohtml5.info/forms.html – Mahoor13 2013-07-21 07:16:52

30
<input type="text" pattern="[a-zA-Z]+" 
oninvalid="setCustomValidity('Plz enter on Alphabets ')" /> 

我在另一篇文章中發現了這段代碼。

+0

謝謝,它爲我工作。雖然我沒有發現帖子或博客或網站提到這一點。 – user219628 2016-01-27 21:31:11

+0

當考慮使用setCustomValitidy注意以下帖子https://stackoverflow.com/questions/16867407/html5-why-does-my-oninvalid-attribute-let-the-pattern-fail – 2017-11-12 09:34:28

76

當使用模式=它會顯示不管你把標題ATTRIB,所以無需JS只是做:

<input type="text" required="" pattern="[0-9]{10}" value="" title="This is an error message" /> 
+8

非常好的答案。但與HTML5的所有內容一樣,可靠性取決於瀏覽器。這個解決方案在FF 15和Chrome 22上運行良好,但Safari 5沒有。(經OS X Lion測試) – 2012-10-08 19:30:30

+1

很好的答案,但要注意這裏的後向(甚至是當前)兼容性。 – bohney 2012-10-08 19:32:28

+6

當我將標題設置爲「foo」時,我得到「請匹配請求的格式.foo」,所以這對我不起作用 – Daan 2014-03-26 08:35:03

3

的setCustomValidity讓您更改默認驗證消息。這裏是如何使用它的簡單例子。

var age = document.getElementById('age'); 
    age.form.onsubmit = function() { 
    age.setCustomValidity("This is not a valid age."); 
}; 
10

HTML:

<input type="text" pattern="[0-9]{10}" oninvalid="InvalidMsg(this);" name="email" oninput="InvalidMsg(this);" /> 

JAVASCRIPT:

function InvalidMsg(textbox) { 

    if(textbox.validity.patternMismatch){ 
     textbox.setCustomValidity('please enter 10 numeric value.'); 
    }  
    else { 
     textbox.setCustomValidity(''); 
    } 
    return true; 
} 

Fiddle Demo

11
<input type="text" pattern="[a-zA-Z]+" 
    oninvalid="setCustomValidity(' ')" 
/> 

剛纔設置的自定義消息,一個空白

12

爲了防止瀏覽器驗證消息出現在文檔中,使用jQuery:210,你可以通過執行以下刪除此警報

$('input, select, textarea').on("invalid", function(e) { 
    e.preventDefault(); 
}); 
1

我找到了一種方法不留神現在: 您可以根據需要使用這樣的:數據錯誤: 「」

<input type="username" class="form-control" name="username" value="" 
placeholder="the least 4 character" 
data-minlength="4" data-minlength-error="the least 4 character" 
data-error="This is a custom Errot Text fot patern and fill blank" 
max-length="15" pattern="[A-Za-z0-9]{4,}" 
title="4~15 character" required/> 
1

正如你可以在這裏看到:

html5 oninvalid doesn't work after fixed the input field

是對你很好以這種方式放置,因爲當你解決錯誤時會消除警告信息。

<input type="text" pattern="[a-zA-Z]+" 
oninvalid="this.setCustomValidity(this.willValidate?'':'your custom message')" /> 
2

我發現Mahoor13答案錯誤,它不是在循環工作,所以我用這個修正固定它:

HTML:

<input type="email" id="eid" name="email_field" oninput="check(this)"> 

的Javascript:

function check(input) { 
    if(input.validity.typeMismatch){ 
     input.setCustomValidity("Dude '" + input.value + "' is not a valid email. Enter something nice!!"); 
    } 
    else { 
     input.setCustomValidity(""); 
    }     
} 

它會pe完美地運行在循環中。

0

這是我的工作在Chrome

<input type="text" name="product_title" class="form-control" 
required placeholder="Product Name" value="" pattern="([A-z0-9À-ž\s]){2,}" 
oninvalid="setCustomValidity('Please enter on Producut Name at least 2 characters long')" 
相關問題