幫我優化正則表達式幫我優化正則表達式
<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(/w/, "1"));
x = (x.replace(/E/, "2"));
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>
重複的:http://stackoverflow.com/questions/3873371/javascript-capture-input-and-convert-characters/3873638#3873638 – BGerrissen 2010-10-06 14:48:38
那豈不是更自然地用「XCV」爲你的最後一行?從左側的S到Z ..有點有趣。 – 2010-10-06 14:56:59
不是我的決定。問RIM! – robert 2010-10-06 15:19:29