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

สำหรับผู้ที่ เริ่มต้น Programming - PHP มีอะไร แนะนำ หรือข้อสงสัยต้องบอร์ด นี้ น่ะค่ะ

Moderator:

acdsin24
Posts: 163
Joined: 01/01/1970 7:00 am
Contact:

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

Postby acdsin24 » 08/05/2006 11:40 am

<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>

Guest

Postby Guest » 17/05/2006 4:00 pm

<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>

acdsin24
Posts: 163
Joined: 01/01/1970 7:00 am
Contact:

Postby acdsin24 » 24/05/2006 5:45 pm

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>

User avatar
icphysics
Posts: 178
Joined: 01/01/1970 7:00 am
Contact:

Postby icphysics » 29/05/2006 12:03 am

อันนี้เว็บ สอนเขียน javascript ครับ ท่าน
http://www.c-point.com/javascript_tutorial/jstutor.htm


Return to “Programming - PHP”

Who is online

Users browsing this forum: No registered users and 1 guest