PDA

View Full Version : اعتبار سنجی فرم با جاوا اسکریپت



heidari68
شنبه 21 تیر 1393, 09:10 صبح
سلام دوستان

من کد زیر رو برای اعتبار سنجی فرم نوشتم که خالی رد نشه.
ولی چک باکس و دکمه های رادیویی و لیست کشویی را نمی دونم باید چه طور چک کنم که خالی رد نشه

ممنونم
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">

function check_form(){


if(document.getElementById('name').value==""){

alert("fill the name") ;

document.getElementById('name').focus();
return false;

}


if(document.getElementById('family').value==""){

alert("fill the family") ;

document.getElementById('family').focus();
return false;

}


if(document.getElementById('username').value==""){

alert("fill the username") ;

document.getElementById('username').focus();
return false;

}


if(document.getElementById('pass').value==""){

alert("fill the pass") ;

document.getElementById('pass').focus();
return false;

}


if(document.getElementById('repass').value==""){

alert("fill the repass") ;

document.getElementById('repass').focus();
return false;

}


if(document.getElementById('q').value==""){

alert("fill the q") ;

document.getElementById('q').focus();
return false;

}


if (document.getElementById('pass').value!=document.g etElementById('repass').value){


alert("not same") ;
document.getElementById('repass').select();
return false;
}





return true;

}

</script>


</head>

<body>
<form action="a.html" method="post" onsubmit="return check_form()">
<table border="1" align="center">


<tr>
<td><input type="text" name="name" id="name" /></td>
<td>نام</td>
<td>*</td>
</tr>
<tr>
<td><input type="text" name="family" id="family"/></td>
<td>فامیلی</td>
<td>*</td>
</tr>
<tr>
<td><input type="text" name="username" id="username"/></td>
<td>نام کاربری</td>
<td>*</td>
</tr>
<tr>
<td><input type="password" name="pass" id="pass" /></td>
<td>رمز</td>
<td>*</td>
</tr>
<tr>
<td><input type="password" name="repass" id="repass" /></td>
<td>تکرار رمز</td>
<td>*</td>
</tr>







<input type="checkbox" id="q" />

<input type="radio" id="e" />

<select>
<option>a</option>
<option>b</option>

</select>






<tr align="center">
<td colspan="3"><input type="reset" value="نوسازی" /><input type="submit" value="ارسال" /></td>
</tr>

</table>

</form>


</body>
</html>

{mahdi}
شنبه 21 تیر 1393, 10:54 صبح
دوست عزیز برای وارد کردن سورس آن را داخل بلاک code قرار دهید که خوانا باشه اینطوری اصلا خونده نمیشه.
لطفا اصلاح کنید.

heidari68
دوشنبه 23 تیر 1393, 08:30 صبح
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">

function check_form(){


if(document.getElementById('name').value==""){

alert("fill the name") ;

document.getElementById('name').focus();
return false;

}


if(document.getElementById('family').value==""){

alert("fill the family") ;

document.getElementById('family').focus();
return false;

}


if(document.getElementById('username').value==""){

alert("fill the username") ;

document.getElementById('username').focus();
return false;

}


if(document.getElementById('pass').value==""){

alert("fill the pass") ;

document.getElementById('pass').focus();
return false;

}


if(document.getElementById('repass').value==""){

alert("fill the repass") ;

document.getElementById('repass').focus();
return false;

}


if(document.getElementById('q').value==""){

alert("fill the q") ;

document.getElementById('q').focus();
return false;

}


if (document.getElementById('pass').value!=document.g etElementById('repass').value){


alert("not same") ;
document.getElementById('repass').select();
return false;
}





return true;

}

</script>


</head>

<body>
<form action="a.html" method="post" onsubmit="return check_form()">
<table border="1" align="center">


<tr>
<td><input type="text" name="name" id="name" /></td>
<td>نام</td>
<td>*</td>
</tr>
<tr>
<td><input type="text" name="family" id="family"/></td>
<td>فامیلی</td>
<td>*</td>
</tr>
<tr>
<td><input type="text" name="username" id="username"/></td>
<td>نام کاربری</td>
<td>*</td>
</tr>
<tr>
<td><input type="password" name="pass" id="pass" /></td>
<td>رمز</td>
<td>*</td>
</tr>
<tr>
<td><input type="password" name="repass" id="repass" /></td>
<td>تکرار رمز</td>
<td>*</td>
</tr>







<input type="checkbox" id="q" />

<input type="radio" id="e" />

<select>
<option>a</option>
<option>b</option>

</select>






<tr align="center">
<td colspan="3"><input type="reset" value="نوسازی" /><input type="submit" value="ارسال" /></td>
</tr>

</table>

</form>


</body>
</html>

miladdn13
سه شنبه 24 تیر 1393, 15:05 عصر
if(!this.form.checkbox.checked)
{
alert('You must agree to the terms first.');
return false;
}

دکمه های رادیویی و چک باکس رو هم از خاصیت پیش فرضشون استفاده کن که یکیو به عنوان پیش فرض انتخاب میکنند

heidari68
سه شنبه 31 تیر 1393, 11:34 صبح
ممنون دوست عزیز از پاسخت

من این رو اضافه کردم ولی جواب نداد خواهش می کنم بیشتر راهنماییم کنید



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">

function check_form(){


if(document.getElementById('name').value==""){

alert("fill the name") ;

document.getElementById('name').focus();
return false;

}


if(document.getElementById('family').value==""){

alert("fill the family") ;

document.getElementById('family').focus();
return false;

}


if(document.getElementById('username').value==""){

alert("fill the username") ;

document.getElementById('username').focus();
return false;

}


if(document.getElementById('pass').value==""){

alert("fill the pass") ;

document.getElementById('pass').focus();
return false;

}


if(document.getElementById('repass').value==""){

alert("fill the repass") ;

document.getElementById('repass').focus();
return false;

}


if(document.getElementById('q').value==""){

alert("fill the q") ;

document.getElementById('q').focus();
return false;

}


if (document.getElementById('pass').value!=document.g etElementById('repass').value){


alert("not same") ;
document.getElementById('repass').select();
return false;
}


if(!this.form.checkbox.checked)
{
alert('You must agree to the terms first.');
return false;
}


return true;

}

</script>


</head>

<body>
<form action="a.html" method="post" onsubmit="return check_form()">
<table border="1" align="center">


<tr>
<td><input type="text" name="name" id="name" /></td>
<td>نام</td>
<td>*</td>
</tr>
<tr>
<td><input type="text" name="family" id="family"/></td>
<td>فامیلی</td>
<td>*</td>
</tr>
<tr>
<td><input type="text" name="username" id="username"/></td>
<td>نام کاربری</td>
<td>*</td>
</tr>
<tr>
<td><input type="password" name="pass" id="pass" /></td>
<td>رمز</td>
<td>*</td>
</tr>
<tr>
<td><input type="password" name="repass" id="repass" /></td>
<td>تکرار رمز</td>
<td>*</td>
</tr>

php<input type="checkbox" name="php" />
html<input type="checkbox" name="html" />



<tr align="center">
<td colspan="3"><input type="reset" value="نوسازی" /><input type="submit" value="ارسال" /></td>
</tr>

</table>

</form>


</body>
</html>

مهرداد سیف زاده
سه شنبه 31 تیر 1393, 12:01 عصر
برای اعتبرا سنجی پیشنهاد میکنم از توابع مفید و درست استفاده کنید
پست زیر میتونه مفید باشه
http://seifzadeh.blog.ir/post/best-javascript-validation