织梦留言统一页面放两个留言板,必填项无效解决方法


    我们在进行织梦仿站的时候,因为需求,可能会将一个页面放两个留言板块,为了避免一些恶意留言,我们一般会用js控制一些必填项。但是一个页面存在两个留言板块之后js就会起冲突。这种问题怎么解决呢?小白可以继续看下去。大佬请关闭此页面,因为方法是在太简单了。

我们先看正常的留言代码与js

html:

<form action="/plus/diy.php" enctype="multipart/form-data" onsubmit="return postcheck()" name="form" id="form" method="post" class="bdd">
          <input type="hidden" name="action" value="post">
          <input type="hidden" name="diyid" value="1">
          <input type="hidden" name="do" value="2">
          <input type="hidden" name="time" id="time" value="">
          <input type="hidden" name="ip" id="ip" value="">
          
          <p class="messLine2">
            <p>姓名:</p>
            <input type="text" name="name" id="name" placeholder="您的姓名">
          </p>
          <p class="messLine2">
            <p>电话:</p>
            <input type="text" name="tel" id="tel" placeholder="您的电话">
          </p>
          <p class="messLine2">
            <p>留言:</p>
            <textarea type="multitext" name="guest" id="guest" cols="33" rows="5" placeholder="我想加盟请来电话告诉我具体细节!谢谢!" onfocus="if(value==&#39;我想加盟请来电话告诉我具体细节!谢谢!&#39;){value=&#39;&#39;}" onblur="if (value==&#39;&#39;){value=&#39;我想加盟请来电话告诉我具体细节!谢谢!&#39;}"></textarea>
          </p>
          <input type="hidden" name="dede_fields" value="name,text;tel,text;guest,multitext;time,textchar;ip,textchar">
          <input type="hidden" name="dede_fieldshash" value="*******************************">
          <button type="submit" name="submit" value="提 交">立即提交</button>
        </form>

js:

	   function postcheck(){
				if (document.form.name.value==""){
					alert('请填写姓名!');
					document.form.name.focus();
					return false;
				}
				var reg1 = /^[\u4e00-\u9fa5]{2,4}$/;
				if (!reg1.test(document.form.name.value)){
					alert('姓名格式不正确,请填写真实姓名!');
					document.form.name.focus();
					return false;
				}
				if (document.form.tel.value==""){
					alert('请填写手机号码!');
					document.form.tel.focus();
					return false;
				}
				var reg2 = /^1[3,4,5,8]\d{9}$/;
				if (!reg2.test(document.form.tel.value)){
					alert('手机号码格式不正确,请填写正确的手机号码!');
					document.form.tel.focus();
					return false;
				}
			}

这样放一个没有问题。放两个怎么解决呢?

其实我们只需要将第二个留言板块html

中的 onsubmit="return postcheck()" name="form"换成其他名字

如:onsubmit="return postcheck2()"  name="form2"

html如下:

<form action="/plus/diy.php" enctype="multipart/form-data" onsubmit="return postcheck2()" name="form2" id="form" method="post" class="bdd">

然后将第二个留言板块的js中所有的

form 换成 form2

function postcheck() 换成 function postcheck2()

js如下:

	   function postcheck2(){
				if (document.form2.name.value==""){
					alert('请填写姓名!');
					document.form2.name.focus();
					return false;
				}
				var reg1 = /^[\u4e00-\u9fa5]{2,4}$/;
				if (!reg1.test(document.form2.name.value)){
					alert('姓名格式不正确,请填写真实姓名!');
					document.form2.name.focus();
					return false;
				}
				if (document.form2.tel.value==""){
					alert('请填写手机号码!');
					document.form2.tel.focus();
					return false;
				}
				var reg2 = /^1[3,4,5,8]\d{9}$/;
				if (!reg2.test(document.form2.tel.value)){
					alert('手机号码格式不正确,请填写正确的手机号码!');
					document.form2.tel.focus();
					return false;
				}
			}

这样一看是不是觉得很简单呢?