Page 1 of 1

JavaScript ง่ายๆ ไว้สำหรับ validate Form

Posted: 08/05/2006 11:40 am
by icphysics
<script language="JavaScript">
function validForm(passForm) {
if (passForm.username.value.replace( /\s+$/, "" )=="")
{
alert("กรุณาใส่ชื่อสมาชิก/Please enter your username" );
passForm.username.focus();
return false;
}
if (passForm.password.value.replace( /\s+$/, "" )=="")
{
alert("กรุณาใส่รหัสผ่าน/Please enter your password" );
passForm.password.focus();
return false;
}
return true;
}

//-->

</script>

Posted: 17/05/2006 4:00 pm
by 4404
<script>
function checkForm() {
if (form.form_username.value=="") {
alert("Username not set");
return false;
}
if (form.form_password.value=="") {
alert("Password not set");
return false;
}
if (form.form_password2.value=="") {
alert("Password not set");
return false;
}
if (form.form_email.value=="") {
alert("E-Mail not set");
return false;
}
if (form.form_password.value!==form.form_password2.value) {
alert("Passwords do not match!");
return false;
}
}
</script>

Posted: 24/05/2006 5:45 pm
by icphysics
valid date ให้อยู่ในรูปแบบ (mm/dd/yyyy):
<script type="text/javascript">
function checkdate(input){
var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
var returnval=false
if (!validformat.test(input.value))
alert("Invalid Date Format. Please correct and submit again.")
else{ //Detailed check for valid date ranges
var monthfield=input.value.split("/")[0]
var dayfield=input.value.split("/")[1]
var yearfield=input.value.split("/")[2]
var dayobj = new Date(yearfield, monthfield-1, dayfield)
if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
alert("Invalid Day, Month, or Year range detected. Please correct and submit again.")
else
returnval=true
}
if (returnval==false) input.select()
return returnval
}

</script>[
ตัวอย่าง Form
<form onSubmit="return checkdate(this.mydate)">
<input type="text" name="mydate" />
<input type="submit" value="submit" /><br />
<b>Valid date format:</b> mm/dd/yyyy
</form>;submit" value="submit">
</form>

Posted: 24/06/2006 12:13 am
by DtTvB
บุคคลทั่วไป wrote:if (form.form_password.value=="") {
หากผู้ใช้พิมพ์เว้นวรรคลงไป ก็จะผ่านครับ
วิธีที่ถูกต้องกว่า คือ if (form.form_username.value.replace(/\s+$/, "") == "") {

Posted: 06/07/2006 3:28 pm
by 4404
icphysics wrote:อันนี้เว็บ สอนเขียน javascript ครับ ท่าน
http://www.c-point.com/javascript_tutorial/jstutor.htm