我创建了包含多个表单字段的表单。根据选择我显示和隐藏表单字段。如何验证移动号码的重复条目
$(document).ready(function() {
$('#hidden-div').hide();
$("#select_btn").change(function() {
toggleFields();
});
});
function toggleFields() {
var selectVal = $("#select_btn").val();
if (selectVal <= 5) {
$hiddenHtml = $('#hidden-div').clone().html();
$("#refer").html('');
for (var i = 0; i < selectVal; i++) {
$("#refer").append($hiddenHtml);
}
}
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<html>
<head>
<title> Demo </title>
<meta name="robots" content="noindex, nofollow" />
<form id ="refer-form" name="refer-form" action="validate.php" method="post" >
<p>No of Referrer:
<select id="select_btn" >
<option value="0">--Select--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<div id="hidden-div">
<div id="text">Referrer</div>
<p>Name:
<input type="text" name="name[]" />
</p>
<p>Mobile:
<input type="text" name="mobile[]" />
</p>
<p>Email:
<input type="text" name="email[]" />
</p>
</div>
<div id="refer">
</div>
<p align="center">
<input type="submit" value="Submit" />
</p>
</form>
&#13;
例如,用户选择No of referrer选项为2,然后我显示表单字段2次....我需要避免重复移动条目....
如果用户再次输入相同的手机,则我抛出错误&#34;请输入有效的手机,否则手机已经输入。&#34;
答案 0 :(得分:2)
方式1:遍历所有手机号码字段,并使用jquery的$.inArray
功能检查以前是否有任何手机号码被占用。
请查看下面的代码段。
$(document).ready(function() {
$('#hidden-div').hide();
$("#select_btn").change(function() {
toggleFields();
});
});
function toggleFields() {
var selectVal = $("#select_btn").val();
if (selectVal <= 5) {
$hiddenHtml = $('#hidden-div').clone().html();
$("#refer").html('');
for (var i = 0; i < selectVal; i++) {
$("#refer").append($hiddenHtml);
}
}
}
$("#refer-form").on('submit',function(){
var phoneNumber = [];
$("input[name='mobile[]']").each(function(){
var _phoneno = $(this).val();
if($.inArray(_phoneno,phoneNumber) === -1){
if($.trim(_phoneno)){
phoneNumber.push(_phoneno);
}
}else{
alert("Mobile number is already exist");
return false;
}
});
$.ajax({
type: "POST",
url: "validate.php",
data:$(this).serialize(),
cache: false,
success: function(data){
console.log(data);
}
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<html>
<head>
<title> Demo </title>
<meta name="robots" content="noindex, nofollow" />
<form id ="refer-form" name="refer-form" action="validate.php" method="post" >
<p>No of Referrer:
<select id="select_btn" >
<option value="0">--Select--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<div id="hidden-div">
<div id="text">Referrer</div>
<p>Name:
<input type="text" name="name[]" />
</p>
<p>Mobile:
<input type="text" name="mobile[]" />
</p>
<p>Email:
<input type="text" name="email[]" />
</p>
</div>
<div id="refer">
</div>
<p align="center">
<input type="submit" value="Submit" />
</p>
</form>
&#13;
Way2 :使用.map
函数获取所有手机号码并使用hasDuplicates
功能检查数组是否包含任何重复值。
$(document).ready(function() {
$('#hidden-div').hide();
$("#select_btn").change(function() {
toggleFields();
});
});
function toggleFields() {
var selectVal = $("#select_btn").val();
if (selectVal <= 5) {
$hiddenHtml = $('#hidden-div').clone().html();
$("#refer").html('');
for (var i = 0; i < selectVal; i++) {
$("#refer").append($hiddenHtml);
}
}
}
$("#refer-form").on('submit',function(){
var phoneNumber = [];
var phoneNumber = $("input[name='mobile[]']").map(function(){if($.trim($(this).val()))return $(this).val();}).get();
if(hasDuplicates(phoneNumber)){
alert("mobile number is already exist");
return false;
}else{
$.ajax({
type: "POST",
url: "validate.php",
data:$(this).serialize(),
cache: false,
success: function(data){
console.log(data);
}
});
}
});
function hasDuplicates(array) {
var valuesSoFar = Object.create(null);
for (var i = 0; i < array.length; ++i) {
var value = array[i];
if (value in valuesSoFar) {
return true;
}
valuesSoFar[value] = true;
}
return false;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<html>
<head>
<title> Demo </title>
<meta name="robots" content="noindex, nofollow" />
<form id ="refer-form" name="refer-form" action="validate.php" method="post" >
<p>No of Referrer:
<select id="select_btn" >
<option value="0">--Select--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<div id="hidden-div">
<div id="text">Referrer</div>
<p>Name:
<input type="text" name="name[]" />
</p>
<p>Mobile:
<input type="text" name="mobile[]" />
</p>
<p>Email:
<input type="text" name="email[]" />
</p>
</div>
<div id="refer">
</div>
<p align="center">
<input type="submit" value="Submit" />
</p>
</form>
&#13;