✔ 最佳答案
以下例子利用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. :)