![](/img/trans.png)
[英]creating html button from javascript with onClick call a method with one param
[英]Change the HTML param from Javascript
我有下面的輸入字段,我有 JS 來驗證在框中輸入的特殊字符的數量。 我有一個自定義幫助文本,一旦將焦點放在輸入框上就會出現。
<input type="text" class="form-control" id="words" name="inputHint" placeholder="Enter synonyms"
data-hint="Enter the words seperated by comma(,)" />
處理驗證的 JS 是
//<![CDATA[
window.onload=function(){
document.getElementById('words').onkeypress = function (e) {
// 46 is the keypress keyCode for period
// http://www.asquare.net/javascript/tests/KeyCode.html
if (e.keyCode === 44 && this.value.split(',').length === 2) {
return false;
}
}
}//]]>
我需要您的幫助,了解如何在return false;
時將data-hint
更改為不同的消息,例如“您按下了兩個逗號” return false;
keyup 事件更適合這種情況,用正則表達式匹配值。
window.onload = function () {
document.getElementById('words').onkeyup = function () {
//event.keyCode - do your work with keyCode
if (this.value.match(/,,/ig)) {
alert("Your message!");
return false;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.