我想捕獲文本框中的輸入字符,然後根據表格進行轉換,並將其作爲用戶類型放回到文本框中。javascript - 捕獲輸入和轉換字符
<form id='myForm'>
Enter phone number:<input type="text" id='idMyText' name="myText" onKeyUp="alphaToNum(this.value)">
</form>
<script>
// on each keypress in input box, I want to capture key pressed,
// determine if key pressed belong to group of identified characters
// if, so then convert to specified numeric equivalent and return character
// to text box.
// This mapping corresponds to numeric characters on blackberry device.
// Normally user has to press alt+letter to get numbers. This will provide
// quicker access to numeric characters on for numeric fields
function alphaToNum(e) {
x = e;
x = (x.replace(/W/, "1"));
x = (x.replace(/E/, "2"));
x = (x.replace(/R/, "3"));
x = (x.replace(/S/, "4"));
x = (x.replace(/D/, "5"));
x = (x.replace(/F/, "6"));
x = (x.replace(/Z/, "7"));
x = (x.replace(/X/, "8"));
x = (x.replace(/C/, "9"));
document.getElementById('idMyText').value = x;
}
</script>
您可能已經注意到了一個旁註,但僅供將來參考:當您驗證或通過JavaScript確保某些字符集時,與未驗證相同。你應該把它當作對用戶的一種很好的服務,但絕不能防止某些人物被張貼。這樣的驗證應該始終伴隨着服務器端的驗證。 – berkes 2010-10-06 14:13:50
是的,謝謝。 – robert 2010-10-06 14:15:54