導航:首頁 > 編程語言 > 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判斷必填相關的資料

熱點內容
哪些統計量可以反映數據波動 瀏覽:946
js地圖標記怎麼清空 瀏覽:479
googlenow教程 瀏覽:508
安全四個責任體系內容文件 瀏覽:965
南陽政務app怎麼注冊 瀏覽:343
照片壓縮包同一路徑的文件能刪嗎 瀏覽:590
路由沒網路怎麼辦 瀏覽:745
南京網站推廣大概多少錢 瀏覽:983
js隱藏列印出來 瀏覽:135
手工之家app哪裡下載 瀏覽:328
網路教育聯盟英語考試是什麼 瀏覽:69
macairwin10忘記密碼 瀏覽:716
java上傳方法 瀏覽:970
csc文件夾可以刪除嗎 瀏覽:130
打線工具怎麼用 瀏覽:376
mat文件如何轉換為cad 瀏覽:761
qq瀏覽器舊版本614 瀏覽:713
將資料庫文件批量導出excel 瀏覽:262
招投標文件在招標公司存多少年 瀏覽:551
u盤空間不足不能復制系統鏡像文件 瀏覽:212

友情鏈接