我想问JavaScript Validation

2007-08-14 7:26 am
如何控制用户者只输入数目和英文字母??
Can gv me javascript code tat control user only key in number and alphabet otherwise will alert user invalid
更新1:

/^[a-zA-Z0-9]*$/; Wat is this mean i mean /^[...]*$/ if no put these symbol what will happen??

回答 (2)

2007-08-14 7:23 pm
✔ 最佳答案
以下例子利用regular expression, 在按submit的时候如果input1栏里有非数目或英文字母的符号,就会弹出一个警告,而且不让form递交。

利用regular expressions可以做到更深入的validation~ ^__^

(请先将 < 后面的多余空格除去再试,那些空格是为了防止Yahoo把code隐藏才加的)
< html>
< head>
< script>
function validate()
{
var patt = /^[a-zA-Z0-9]*$/;
if (document.form1.input1.value.search(patt) == -1)
{
alert("Only numbers and/or alphabets are allowed");
return false;
}
else
{
return true;
}
}
< /script>
< /head>
< body>
< form action="abc.html" name="form1" onsubmit="return validate(); " >
Please enter only numbers and/or alphabets:
< br />
< input type=text name="input1" />
< br />
< input type="submit" />
< /form >
< /body >
< /html >

2007-08-15 12:33:15 補充:
/^[a-zA-Z0-9]*$/ matches for a string that has 0 or more alpha-numerals from the beginning to the end. (/.../ means ... is a pattern)If you put /a-zA-Z0-9/ it will be looking the string "a-zA-Z0-9" instead. Regular Expressions is a huge topic.. There’s lots of info online though. :)
2007-08-14 6:55 pm


收錄日期: 2021-04-23 17:11:45
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070813000051KK06767

檢視 Wayback Machine 備份