Commit c7dbe8b2 by Dinh Thanh Truc

basic form

parent e1b702c4
var main=function(){
$('#register').click(function(){
console.log('click');
$('.error').hide();
var firstName=$('#firstName').val();
var lastName=$('#lastName').val();
var email=$('#email').val();
var emailConfirm=$('#emailConfirm').val();
var birthday=$('#birthday').val();
var reg=true;
if(!(firstName)||!((/^[a-z\s]+$/i).test(firstName))){
$('.error').show();
$('#firstName').val('Error!');
reg=false;
};
if(!(lastName)||!((/^[a-z\s]+$/i).test(lastName))){
$('.error').show();
$('#lastName').val('Error!');
reg=false;
};
if(!((/^[a-z][a-z0-9._]+@[a-z0-9]+\.[a-z0-9.]+[a-z]+$/i).test(email))){
$('.error').show();
$('#email').val('Error!');
reg=false;
};
if(!((/^[a-z][a-z0-9._]+@[a-z0-9]+\.[a-z0-9.]+[a-z]+$/i).test(emailConfirm))||email!=emailConfirm){
$('.error').show();
$('#emailConfirm').val('Error!');
reg=false;
};
if(!checkDate(birthday)){
$('.error').show();
$('#birthday').val('Error!');
reg=false;
}
if (reg===true) {
confirm("Thank you for registing");
}
});
}
$(document).ready(main);
  • Nên phân biệt giữa nhập sai giá trị và để trống. Khi input có chữ Error! click chuột vào phải mất chữ Error! Hoặc có thể thêm cụ thể vị trí error lên class error. Ví dụ:

    • First name error
    • Last name error
    Edited by Nguyen Ngoc Minh Thao
Please register or sign in to reply
var checkDate=function(date){
var regDate=/^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d+)$/;
var matchArray=date.match(regDate);
if (matchArray){
if((matchArray[1]<1)||(matchArray[1]>31)){
return false;
};
if ((matchArray[3]<1)||(matchArray[3]>12)){
return false;
};
if (matchArray[5].length!=4){
return false;
};
return true;
};
return false;
}
<!Doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript" src="app.js"></script>
<link type="text/css" rel="stylesheet" href="stylesheet.css">
<title>Register Form</title>
</head>
<body>
<div class="formDiv">
<h1 style="text-align: center;" class="header1">Register Form</h1>
Please register or sign in to reply
<table>
<tr style="height:20px">
<td></td>
<td>
<p class="error">Input error</p>
</td>
</tr>
<tr>
<td class="labelTd"><label for="firstName" class="registLabel" >First Name</label></td>
<td><input class="registInput" id="firstName" placeholder="First Name" /></td>
Please register or sign in to reply
</tr>
<tr>
<td class="labelTd"><label for="lastName" class="registLabel" >Last Name</label></td>
<td><input class="registInput" id="lastName" placeholder="Last Name" /></td>
</tr>
<td class="labelTd"><label for="email" class="registLabel" >Email</label></td>
<td><input class="registInput" id="email" placeholder="Email" /></td>
<tr>
<td class="labelTd"><label for="emailConfirm" class="registLabel" >Email Confirmation</label></td>
<td><input class="registInput" id="emailConfirm" placeholder="Email Confirmation" /></td>
</tr>
<tr>
<td class="labelTd"><label for="birthday" class="registLabel" >Birthday</label></td>
<td><input class="registInput" id="birthday" placeholder="Birthday" /></td>
</tr>
<tr>
<td></td>
<td><button type="button" id="register">Register</button></td>
Please register or sign in to reply
</tr>
</table>
</div>
</body>
</html>
.formDiv{
margin: 0 auto;
width:1171px;
height:435px;
border:1px solid
}
.header1{
font-family: Arial;
  • font-family, font-size dùng chung cho page nên để chung css. Ví dụ: html, body {font-family: Arial; font-size:14px;}

Please register or sign in to reply
font-size: 28px;
font-weight:normal;
margin-top: 20px;
margin-bottom: 10px
}
.error{
font-size:14px;
font-family: Arial;
display: none;
padding:0;
margin:0;
background-color:pink
}
.registLabel{
font-size:14px;
font-family: Arial;
padding-right:15px;
float:right;
}
.registInput{
width:944px;
height:32px;
padding-left: 10px;
margin-top: 8px;
margin-bottom: 8px;
border-radius: 5px;
border:1px solid gray;
font-size:14px;
font-family: Arial;
}
::-webkit-input-placeholder{
font-size:14px;
font-family: Arial;
}
.labelTd{
width:183px;
background-color:white;
font-weight:bold;
}
#register{
  • type css hạn chế dùng ID. Trong trường hợp này có thể dùng: table button

Please register or sign in to reply
width:77px;
height:33px;
font-size:14px;
background-color:white;
border-radius: 5px;
border:1px solid gray;
margin-top: 8px;
margin-bottom: 8px;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment