JavaScript basis (b)
## a, in peace we need to verify whether the book output to meet the requirements, then the figures how to solve:
< Body >
< H2 > the JavaScript can verify input </ H2 >
< P > Please enter a number between 1 and 10: </ P >
< INPUT ID = "Numb" >
< Button type = "Button" the onclick = "myFunction () " > submit </ Button >
< P ID =" Demo " > </ P >
< Script >
function myFunction () {
var X, text;
// Gets the value of id = "numb" in the input field
x = document.getElementById ( " Numb " ) .Value;
// if x is not less than 1 or greater than or number 10
IF (isNaN (x) || x < 1 || x > 10 ) {
text = " Invalid input " ;
} the else {
text = " enter valid " ;
}
document.getElementById ( " Demo " ) .innerHTML = text;
}
</ Script >
</ body>
## Second, the registration page, enter how to verify compliance with the requirements of:
<tr>
<td class="td_left"><label for="username">用户名:</label></td>
<td class="td_right"><input type="text" name="username" id="username" placeholder="请输入用户名" onblur="checkUserName()">
<span id="s_username"class="error"style="color: red"></span>
</td>
</tr>
<tr>
<td class="td_left"><label for="password">密码:</label></td>
<td class="td_right"><input type="password" name="password" id="password" placeholder="请输入密码"onblur="checkPassWord()">
<span id="s_password"class="error"></span>
</td>
</tr>
So we javaScript code in the method:
<script type="text/javascript">
function checkUserName() {
var username=document.getElementById("username").value;//获得username id的对象
var reg_username=/^\w{6,12}$/;
var flag=reg_username.test(username);
var s_username=document.getElementById("s_username");
if(flag){
s_username.innerHTML="<img src='gou.png'>";
}else{
s_username.innerHTML="用户名格式错误";
}
}
function checkPassWord() {
var password=document.getElementById("password").value;
var reg_password=/^\w{6,12}$/;
var flag=reg_password.test(password);
var s_password=document.getElementById("s_password");
if(flag){
s_password.innerHTML="<img src='gou.png'>";
}else{
s_password.innerHTML="密码格式错误,请重新输入";
}
}
</script>