2013年10月27日 星期日

Homework 10-21-2013

試了兩個方法....
法(1):
<html>
<head>
</head>
<body id="body">
<form action="javascript:void(0);" id="exampleForm">
<input type="text" id="examplePass"/>
<input type="submit" />
</form>
</body>
<script>
document.getElementById("exampleForm").onsubmit = function(){
var passwordRegex =/^-?\d*$/;
if(!passwordRegex.test(document.getElementById("examplePass").value)){
console.log("Regex didn't match");
var notify = document.getElementById("notify");
if(notify === null){
notify = document.createElement("p");
notify.textContent = "error character";
notify.id ="notify";
var body =document.getElementById("body");
body.appendChild(notify);
}
}
};
</script>
</html>


法(2)

<html>
<body>
<script>
function f_check_float(obj)
{
if (/^\d+$/.test( obj.value ))
{ alert("輸入為大於等於0的整數字串");

return true;
}
if (/^((-\d+)|(0+))$/.test( obj.value ))
{
alert("輸入的是負整數");
return true;
}
else
{
alert("请输入正确的字串");
return false;
}
}
</script>
<form>
<input name="vvv">
<input type="button" value="檢查字串" onClick="javascript:f_check_float(vvv)"/>
</form>
</body>
</html>





沒有留言:

張貼留言