導航:首頁 > 編程語言 > js判斷必填

js判斷必填

發布時間:2023-08-21 18:44:16

js中選中select的某項值後,判斷其他文本框是否為必填。。急急急。。在線等。

這段代碼的優點是隨便你定義多少個自定義化必須項目。。。。

先解說下:一般以

===============

if(form.ly_name.value == "")
{
alert("請填寫您的大名!"); // js提示
form.ly_name.focus();
return false;
}

只要 改下name名稱 多次利用可循環標簽

===============

return true; //結尾

===============

其中ly_name是表單中某個你需要瀏覽者必填的項目name名稱



<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>無標題文檔</title>
</head>

<body>
<formaction=""method="post"name="form"onSubmit="returnCheckForm(this);">
<tablewidth="70%"border="0"cellpadding="3"cellspacing="1"bgcolor="#b4c6db">
<tbody>
<tr>
<tdwidth="15%"height="30"class="newly_name">您的大名</td>
<tdcolspan="搜告2"class="ly_title2"><inputname="ly_name"type="text"tabindex="1"onMouseOver="this.className='input_1'"
onMouseOut="this.className='input_2'"/><歲漏嫌fontclass="redstar">*</font><fontclass="zhu">[帶<fontclass="redstar">*</font>部
分為必填項]</font></td>
<tdwidth="16%"rowspan="4"style="background:#fff;"class="ly_title2"align="right">&nbsp;</td>
</tr>

<trheight="30">
<tdclass="newly_sex">您的乎手性別:</td>
<tdcolspan="2"class="ly_title2">
<imgsrc="images/boy.gif"/><inputname="ly_sex"type="radio"value="先生"checked/>先生
<imgsrc="images/girl.gif"/><inputname="ly_sex"type="radio"value="女士/">女士</td>
</tr>

<tr>
<tdclass="newly_qq"height="30">您的QQ:</td>
<tdcolspan="2"class="ly_title2"><inputtype="text"tabindex="2"onMouseOver="this.className='input_1'"
onMouseOut="this.className='input_2'"/><fontclass="redstar">*</font></td>
</tr>


<tr>
<tdclass="newly_title">留言標題:</td>
<tdcolspan="2"class="ly_title2"><inputname="ly_title"type="text"tabindex="3"onMouseOver="this.className='input_1'"
onMouseOut="this.className='input_2'"/><fontclass="redstar">*</font></td>
</tr>
<tr>
<tdclass="newly_content">留言內容:</td>
<tdcolspan="3"class="ly_title2"><textareaname="ly_content"cols="70"rows="15"tabindex="5"
style="background:#fffurl(images/notepad.jpg)rightcenterno-repeat;"onMouseOver="this.className='input_1'"
onMouseOut="this.className='input_2'"></textarea><fontclass="redstar">*</font></td>
</tr>

<trheight="30">
<tdclass="ly_title">&nbsp;</td>
<tdwidth="31%"class="ly_title2"><spanclass="ly_title">
<inputvalue="提交留言"class="inp_L1"onMouseOver="this.className='inp_L2'"
onMouseOut="this.className='inp_L1'"id="input_btn2"name="input_btn"tabindex="6"type="submit"onClick="form.submit()"onKeyDown="if(event.keyCode==13)form.submit()"/>
</span></td>
<tdwidth="38%"class="ly_title2"><inputvalue="清空再來"class="inp_L1"onMouseOver="this.className='inp_L2'"onMouseOut="this.className='inp_L1'"id="input_btn1"name="enter.x"tabindex="7"type="reset"/></td>
<tdclass="ly_title2">&nbsp;</td>
</tr>
</tbody>
</table>
</form>

<SCRIPTlanguage=javaScript>
functionCheckForm(form)
{

if(form.ly_name.value=="")
{
alert("請填寫您的大名!");
form.ly_name.focus();
returnfalse;
}

if(form.ly_qq.value=="")
{
alert("請填寫您的QQ!");
form.ly_qq.focus();
returnfalse;
}

if(form.ly_title.value=="")
{
alert("請填寫您的留言標題!");
form.ly_title.focus();
returnfalse;
}

if(form.ly_content.value=="")
{
alert("請填寫留言內容!");
form.ly_content.focus();
returnfalse;
}

if(form.ly_content.value.length>500)
{
alert("詳情內容不能超過500字!");
form.ly_content.focus();
returnfalse;
}

varregx=/^d{5,9}$/;//^表示不匹配。d表示任意數字,{5,9}表示長度為5到9。
if(!regx.test(form.ly_qq.value)){
alert("請輸入你正確的QQ號");
form.ly_qq.focus();
returnfalse;
}//用上面定義的正則表達式測試,如果不匹配則返回false,否則返回true
returntrue;//結尾
}
</SCRIPT>
</body>
</html>

㈡ js驗證表單

avaScript 表單驗證

JavaScript 可用來在數據被送往伺服器前對 HTML 表單中的這些輸入數據進行驗證。

被 JavaScript 驗證的這些典型的表單數據有:

㈢ 怎麼用JS做form表單驗證,要詳細代碼,求救!(帶星號的是必填項)

舉個例子吧:
<input type=」text」 id=」telephone」 >
<input type=「button」 onclick=「functioncheck1」value=「檢測」>
如果要檢測它是否符合標准,那我就會在頁面中導入js包然後寫一個
<script>
functioncheck1()
{
varreg1 = /^1(5|3|8)\d{9}$/;
varstr1 = document.getElementById("telephone").value;
if(reg1.test(str1)== true)
{
alert("手機號碼OK");
}
else
{
alert("請重新輸入手機號碼");
}
}
</script>
就可以驗證到了!

閱讀全文

與js判斷必填相關的資料

熱點內容
excel是啥類型文件 瀏覽:471
安慶培訓編程怎麼選 瀏覽:817
vs2010mfc工具箱 瀏覽:402
刪除文件重置win10怎麼恢復 瀏覽:180
新裝win10有線連不上 瀏覽:785
資料庫商品名唯一怎麼標注 瀏覽:214
電信5g網路什麼時候出 瀏覽:626
手機如何把視頻打包到文件夾裡面 瀏覽:248
excel數據轉vcf文件 瀏覽:585
編程題如何左右翻轉 瀏覽:920
wp10微信安裝不了 瀏覽:793
微軟資料庫密碼怎麼改 瀏覽:627
我們是貴族什麼app可以看 瀏覽:65
哪個app認字免費 瀏覽:964
少兒編程線上教育如何 瀏覽:137
ps3升級檢測不到優盤升級文件 瀏覽:845
vs連接資料庫顯示數據 瀏覽:867
win10的設備和列印機 瀏覽:876
linux最新 瀏覽:970
蘋果6有時候自動重啟 瀏覽:851

友情鏈接