Php Jquery表单验证-动态表单名称
我需要一些帮助来验证我的表单:Php Jquery表单验证-动态表单名称,php,jquery,validation,Php,Jquery,Validation,我需要一些帮助来验证我的表单: $(document).ready(function() { $("#set_search").validate({ rules: { q: { required: true, minlength: 2 }, q2: {
$(document).ready(function() {
$("#set_search").validate({
rules: {
q: {
required: true,
minlength: 2
},
q2: {
number: true
},
},
messages: {
q: "Please enter your name",
q2: "Only numbers allowed",
}
});
});
$(document).ready(function() {
$('form[id*="set_search"]').validate({
rules: {
q: {
required: true,
minlength: 2
},
q2: {
email: true
},
},
messages: {
q1: "Please enter your name",
q2: "Please enter a valid email address",
}
});
});
</script>
<style type="text/css">
.error {
color: red;
font: 12pt verdana;
padding-left: 10px
}
.input {
margin-top:10px;
margin-left:20px;
}
</style>
</head>
<?php include("config/kapcsolat.inc");?>
<body>
<?php
for($i=0;$i<3;$i++) {
echo "<p><div class='showhide$i'><form name='set_search".$i."' id='set_search".$i." ' action='settings.php' method='get'><p><label for='q1'>name</label><input type='text' id='q' name='q'/></p><p><label for='q2'>uniqid</label>
<input type='text' id='q2' name='q2' /></p><p><input type='hidden' id='q3' name='q3' value='1' /></p><p><input type='submit' value='Search'/></p></form></div></p>";
}
?>
</body>
</html>
现在,我使用以下代码验证我的表单:
$(document).ready(function() {
$("#set_search").validate({
rules: {
q: {
required: true,
minlength: 2
},
q2: {
number: true
},
},
messages: {
q: "Please enter your name",
q2: "Only numbers allowed",
}
});
});
$(document).ready(function() {
$('form[id*="set_search"]').validate({
rules: {
q: {
required: true,
minlength: 2
},
q2: {
email: true
},
},
messages: {
q1: "Please enter your name",
q2: "Please enter a valid email address",
}
});
});
</script>
<style type="text/css">
.error {
color: red;
font: 12pt verdana;
padding-left: 10px
}
.input {
margin-top:10px;
margin-left:20px;
}
</style>
</head>
<?php include("config/kapcsolat.inc");?>
<body>
<?php
for($i=0;$i<3;$i++) {
echo "<p><div class='showhide$i'><form name='set_search".$i."' id='set_search".$i." ' action='settings.php' method='get'><p><label for='q1'>name</label><input type='text' id='q' name='q'/></p><p><label for='q2'>uniqid</label>
<input type='text' id='q2' name='q2' /></p><p><input type='hidden' id='q3' name='q3' value='1' /></p><p><input type='submit' value='Search'/></p></form></div></p>";
}
?>
</body>
</html>
我有这个搜索表,在for循环中:
for ($i=0;$i<5;$i++) {
echo "<div class='showhide$i'><form name='set_search' id='set_search' action='settings.php' method='get'><p><label for='q1'>name</label><input type='text' id='q' name='q'/></p><p><label for='q2'>number</label><input type='text' id='q2' name='q2' /></p><p><input type='submit' value='Search'/></p></form></div></p>";
}
$(document).ready(function() {
$('form[id*="set_search"]').validate({
rules: {
q: {
required: true,
minlength: 2
},
q2: {
email: true
},
},
messages: {
q1: "Please enter your name",
q2: "Please enter a valid email address",
}
});
});
</script>
<style type="text/css">
.error {
color: red;
font: 12pt verdana;
padding-left: 10px
}
.input {
margin-top:10px;
margin-left:20px;
}
</style>
</head>
<?php include("config/kapcsolat.inc");?>
<body>
<?php
for($i=0;$i<3;$i++) {
echo "<p><div class='showhide$i'><form name='set_search".$i."' id='set_search".$i." ' action='settings.php' method='get'><p><label for='q1'>name</label><input type='text' id='q' name='q'/></p><p><label for='q2'>uniqid</label>
<input type='text' id='q2' name='q2' /></p><p><input type='hidden' id='q3' name='q3' value='1' /></p><p><input type='submit' value='Search'/></p></form></div></p>";
}
?>
</body>
</html>
对于($i=0;$i只需将类名附加到必须验证的表单。这应该可以找到id中包含“set\u search”的所有表单:
$('form[id*="set_search"]').validate();
$('form[id^="set_search"]').validate();
$(document).ready(function() {
$('form[id*="set_search"]').validate({
rules: {
q: {
required: true,
minlength: 2
},
q2: {
email: true
},
},
messages: {
q1: "Please enter your name",
q2: "Please enter a valid email address",
}
});
});
</script>
<style type="text/css">
.error {
color: red;
font: 12pt verdana;
padding-left: 10px
}
.input {
margin-top:10px;
margin-left:20px;
}
</style>
</head>
<?php include("config/kapcsolat.inc");?>
<body>
<?php
for($i=0;$i<3;$i++) {
echo "<p><div class='showhide$i'><form name='set_search".$i."' id='set_search".$i." ' action='settings.php' method='get'><p><label for='q1'>name</label><input type='text' id='q' name='q'/></p><p><label for='q2'>uniqid</label>
<input type='text' id='q2' name='q2' /></p><p><input type='hidden' id='q3' name='q3' value='1' /></p><p><input type='submit' value='Search'/></p></form></div></p>";
}
?>
</body>
</html>
这一个应该找到所有以set_search'作为其id开头的表单:
$('form[id*="set_search"]').validate();
$('form[id^="set_search"]').validate();
$(document).ready(function() {
$('form[id*="set_search"]').validate({
rules: {
q: {
required: true,
minlength: 2
},
q2: {
email: true
},
},
messages: {
q1: "Please enter your name",
q2: "Please enter a valid email address",
}
});
});
</script>
<style type="text/css">
.error {
color: red;
font: 12pt verdana;
padding-left: 10px
}
.input {
margin-top:10px;
margin-left:20px;
}
</style>
</head>
<?php include("config/kapcsolat.inc");?>
<body>
<?php
for($i=0;$i<3;$i++) {
echo "<p><div class='showhide$i'><form name='set_search".$i."' id='set_search".$i." ' action='settings.php' method='get'><p><label for='q1'>name</label><input type='text' id='q' name='q'/></p><p><label for='q2'>uniqid</label>
<input type='text' id='q2' name='q2' /></p><p><input type='hidden' id='q3' name='q3' value='1' /></p><p><input type='submit' value='Search'/></p></form></div></p>";
}
?>
</body>
</html>
任何一个都应该解决你的问题。尽管Kieran Hayes的回答如此
<form class="myxform"... , $(".myxform").validate({...
$(document).ready(function() {
$('form[id*="set_search"]').validate({
rules: {
q: {
required: true,
minlength: 2
},
q2: {
email: true
},
},
messages: {
q1: "Please enter your name",
q2: "Please enter a valid email address",
}
});
});
</script>
<style type="text/css">
.error {
color: red;
font: 12pt verdana;
padding-left: 10px
}
.input {
margin-top:10px;
margin-left:20px;
}
</style>
</head>
<?php include("config/kapcsolat.inc");?>
<body>
<?php
for($i=0;$i<3;$i++) {
echo "<p><div class='showhide$i'><form name='set_search".$i."' id='set_search".$i." ' action='settings.php' method='get'><p><label for='q1'>name</label><input type='text' id='q' name='q'/></p><p><label for='q2'>uniqid</label>
<input type='text' id='q2' name='q2' /></p><p><input type='hidden' id='q3' name='q3' value='1' /></p><p><input type='submit' value='Search'/></p></form></div></p>";
}
?>
</body>
</html>
或
$(document).ready(function() {
$('form[id*="set_search"]').validate({
rules: {
q: {
required: true,
minlength: 2
},
q2: {
email: true
},
},
messages: {
q1: "Please enter your name",
q2: "Please enter a valid email address",
}
});
});
</script>
<style type="text/css">
.error {
color: red;
font: 12pt verdana;
padding-left: 10px
}
.input {
margin-top:10px;
margin-left:20px;
}
</style>
</head>
<?php include("config/kapcsolat.inc");?>
<body>
<?php
for($i=0;$i<3;$i++) {
echo "<p><div class='showhide$i'><form name='set_search".$i."' id='set_search".$i." ' action='settings.php' method='get'><p><label for='q1'>name</label><input type='text' id='q' name='q'/></p><p><label for='q2'>uniqid</label>
<input type='text' id='q2' name='q2' /></p><p><input type='hidden' id='q3' name='q3' value='1' /></p><p><input type='submit' value='Search'/></p></form></div></p>";
}
?>
</body>
</html>
---编辑
$(document).ready(function() {
$('form[id*="set_search"]').validate({
rules: {
q: {
required: true,
minlength: 2
},
q2: {
email: true
},
},
messages: {
q1: "Please enter your name",
q2: "Please enter a valid email address",
}
});
});
</script>
<style type="text/css">
.error {
color: red;
font: 12pt verdana;
padding-left: 10px
}
.input {
margin-top:10px;
margin-left:20px;
}
</style>
</head>
<?php include("config/kapcsolat.inc");?>
<body>
<?php
for($i=0;$i<3;$i++) {
echo "<p><div class='showhide$i'><form name='set_search".$i."' id='set_search".$i." ' action='settings.php' method='get'><p><label for='q1'>name</label><input type='text' id='q' name='q'/></p><p><label for='q2'>uniqid</label>
<input type='text' id='q2' name='q2' /></p><p><input type='hidden' id='q3' name='q3' value='1' /></p><p><input type='submit' value='Search'/></p></form></div></p>";
}
?>
</body>
</html>
Javascrit:
$(document).ready(function() {
$("form[id^='set_search']").each(function(){
alert($(this).attr("id"));
$(this).validate({
rules: {
q: {required: true, minlength: 2},
q2: {number: true}
},
messages: {
q: "Please enter your name",
q2: "Only numbers allowed"
}
});
});
});
$(document).ready(function() {
$('form[id*="set_search"]').validate({
rules: {
q: {
required: true,
minlength: 2
},
q2: {
email: true
},
},
messages: {
q1: "Please enter your name",
q2: "Please enter a valid email address",
}
});
});
</script>
<style type="text/css">
.error {
color: red;
font: 12pt verdana;
padding-left: 10px
}
.input {
margin-top:10px;
margin-left:20px;
}
</style>
</head>
<?php include("config/kapcsolat.inc");?>
<body>
<?php
for($i=0;$i<3;$i++) {
echo "<p><div class='showhide$i'><form name='set_search".$i."' id='set_search".$i." ' action='settings.php' method='get'><p><label for='q1'>name</label><input type='text' id='q' name='q'/></p><p><label for='q2'>uniqid</label>
<input type='text' id='q2' name='q2' /></p><p><input type='hidden' id='q3' name='q3' value='1' /></p><p><input type='submit' value='Search'/></p></form></div></p>";
}
?>
</body>
</html>
HTML:
$(document).ready(function() {
$('form[id*="set_search"]').validate({
rules: {
q: {
required: true,
minlength: 2
},
q2: {
email: true
},
},
messages: {
q1: "Please enter your name",
q2: "Please enter a valid email address",
}
});
});
</script>
<style type="text/css">
.error {
color: red;
font: 12pt verdana;
padding-left: 10px
}
.input {
margin-top:10px;
margin-left:20px;
}
</style>
</head>
<?php include("config/kapcsolat.inc");?>
<body>
<?php
for($i=0;$i<3;$i++) {
echo "<p><div class='showhide$i'><form name='set_search".$i."' id='set_search".$i." ' action='settings.php' method='get'><p><label for='q1'>name</label><input type='text' id='q' name='q'/></p><p><label for='q2'>uniqid</label>
<input type='text' id='q2' name='q2' /></p><p><input type='hidden' id='q3' name='q3' value='1' /></p><p><input type='submit' value='Search'/></p></form></div></p>";
}
?>
</body>
</html>
名字
$(document).ready(function() {
$('form[id*="set_search"]').validate({
rules: {
q: {
required: true,
minlength: 2
},
q2: {
email: true
},
},
messages: {
q1: "Please enter your name",
q2: "Please enter a valid email address",
}
});
});
</script>
<style type="text/css">
.error {
color: red;
font: 12pt verdana;
padding-left: 10px
}
.input {
margin-top:10px;
margin-left:20px;
}
</style>
</head>
<?php include("config/kapcsolat.inc");?>
<body>
<?php
for($i=0;$i<3;$i++) {
echo "<p><div class='showhide$i'><form name='set_search".$i."' id='set_search".$i." ' action='settings.php' method='get'><p><label for='q1'>name</label><input type='text' id='q' name='q'/></p><p><label for='q2'>uniqid</label>
<input type='text' id='q2' name='q2' /></p><p><input type='hidden' id='q3' name='q3' value='1' /></p><p><input type='submit' value='Search'/></p></form></div></p>";
}
?>
</body>
</html>
数
$(document).ready(function() {
$('form[id*="set_search"]').validate({
rules: {
q: {
required: true,
minlength: 2
},
q2: {
email: true
},
},
messages: {
q1: "Please enter your name",
q2: "Please enter a valid email address",
}
});
});
</script>
<style type="text/css">
.error {
color: red;
font: 12pt verdana;
padding-left: 10px
}
.input {
margin-top:10px;
margin-left:20px;
}
</style>
</head>
<?php include("config/kapcsolat.inc");?>
<body>
<?php
for($i=0;$i<3;$i++) {
echo "<p><div class='showhide$i'><form name='set_search".$i."' id='set_search".$i." ' action='settings.php' method='get'><p><label for='q1'>name</label><input type='text' id='q' name='q'/></p><p><label for='q2'>uniqid</label>
<input type='text' id='q2' name='q2' /></p><p><input type='hidden' id='q3' name='q3' value='1' /></p><p><input type='submit' value='Search'/></p></form></div></p>";
}
?>
</body>
</html>
$(document).ready(function() {
$('form[id*="set_search"]').validate({
rules: {
q: {
required: true,
minlength: 2
},
q2: {
email: true
},
},
messages: {
q1: "Please enter your name",
q2: "Please enter a valid email address",
}
});
});
</script>
<style type="text/css">
.error {
color: red;
font: 12pt verdana;
padding-left: 10px
}
.input {
margin-top:10px;
margin-left:20px;
}
</style>
</head>
<?php include("config/kapcsolat.inc");?>
<body>
<?php
for($i=0;$i<3;$i++) {
echo "<p><div class='showhide$i'><form name='set_search".$i."' id='set_search".$i." ' action='settings.php' method='get'><p><label for='q1'>name</label><input type='text' id='q' name='q'/></p><p><label for='q2'>uniqid</label>
<input type='text' id='q2' name='q2' /></p><p><input type='hidden' id='q3' name='q3' value='1' /></p><p><input type='submit' value='Search'/></p></form></div></p>";
}
?>
</body>
</html>
名字
$(document).ready(function() {
$('form[id*="set_search"]').validate({
rules: {
q: {
required: true,
minlength: 2
},
q2: {
email: true
},
},
messages: {
q1: "Please enter your name",
q2: "Please enter a valid email address",
}
});
});
</script>
<style type="text/css">
.error {
color: red;
font: 12pt verdana;
padding-left: 10px
}
.input {
margin-top:10px;
margin-left:20px;
}
</style>
</head>
<?php include("config/kapcsolat.inc");?>
<body>
<?php
for($i=0;$i<3;$i++) {
echo "<p><div class='showhide$i'><form name='set_search".$i."' id='set_search".$i." ' action='settings.php' method='get'><p><label for='q1'>name</label><input type='text' id='q' name='q'/></p><p><label for='q2'>uniqid</label>
<input type='text' id='q2' name='q2' /></p><p><input type='hidden' id='q3' name='q3' value='1' /></p><p><input type='submit' value='Search'/></p></form></div></p>";
}
?>
</body>
</html>
数
$(document).ready(function() {
$('form[id*="set_search"]').validate({
rules: {
q: {
required: true,
minlength: 2
},
q2: {
email: true
},
},
messages: {
q1: "Please enter your name",
q2: "Please enter a valid email address",
}
});
});
</script>
<style type="text/css">
.error {
color: red;
font: 12pt verdana;
padding-left: 10px
}
.input {
margin-top:10px;
margin-left:20px;
}
</style>
</head>
<?php include("config/kapcsolat.inc");?>
<body>
<?php
for($i=0;$i<3;$i++) {
echo "<p><div class='showhide$i'><form name='set_search".$i."' id='set_search".$i." ' action='settings.php' method='get'><p><label for='q1'>name</label><input type='text' id='q' name='q'/></p><p><label for='q2'>uniqid</label>
<input type='text' id='q2' name='q2' /></p><p><input type='hidden' id='q3' name='q3' value='1' /></p><p><input type='submit' value='Search'/></p></form></div></p>";
}
?>
</body>
</html>
$(document).ready(function() {
$('form[id*="set_search"]').validate({
rules: {
q: {
required: true,
minlength: 2
},
q2: {
email: true
},
},
messages: {
q1: "Please enter your name",
q2: "Please enter a valid email address",
}
});
});
</script>
<style type="text/css">
.error {
color: red;
font: 12pt verdana;
padding-left: 10px
}
.input {
margin-top:10px;
margin-left:20px;
}
</style>
</head>
<?php include("config/kapcsolat.inc");?>
<body>
<?php
for($i=0;$i<3;$i++) {
echo "<p><div class='showhide$i'><form name='set_search".$i."' id='set_search".$i." ' action='settings.php' method='get'><p><label for='q1'>name</label><input type='text' id='q' name='q'/></p><p><label for='q2'>uniqid</label>
<input type='text' id='q2' name='q2' /></p><p><input type='hidden' id='q3' name='q3' value='1' /></p><p><input type='submit' value='Search'/></p></form></div></p>";
}
?>
</body>
</html>
现在应该可以了,但有点不对劲
$(document).ready(function() {
$('form[id*="set_search"]').validate({
rules: {
q: {
required: true,
minlength: 2
},
q2: {
email: true
},
},
messages: {
q1: "Please enter your name",
q2: "Please enter a valid email address",
}
});
});
</script>
<style type="text/css">
.error {
color: red;
font: 12pt verdana;
padding-left: 10px
}
.input {
margin-top:10px;
margin-left:20px;
}
</style>
</head>
<?php include("config/kapcsolat.inc");?>
<body>
<?php
for($i=0;$i<3;$i++) {
echo "<p><div class='showhide$i'><form name='set_search".$i."' id='set_search".$i." ' action='settings.php' method='get'><p><label for='q1'>name</label><input type='text' id='q' name='q'/></p><p><label for='q2'>uniqid</label>
<input type='text' id='q2' name='q2' /></p><p><input type='hidden' id='q3' name='q3' value='1' /></p><p><input type='submit' value='Search'/></p></form></div></p>";
}
?>
</body>
</html>
$(document).ready(function() {
$('form[id*="set_search"]').validate({
rules: {
q: {
required: true,
minlength: 2
},
q2: {
email: true
},
},
messages: {
q1: "Please enter your name",
q2: "Please enter a valid email address",
}
});
});
</script>
<style type="text/css">
.error {
color: red;
font: 12pt verdana;
padding-left: 10px
}
.input {
margin-top:10px;
margin-left:20px;
}
</style>
</head>
<?php include("config/kapcsolat.inc");?>
<body>
<?php
for($i=0;$i<3;$i++) {
echo "<p><div class='showhide$i'><form name='set_search".$i."' id='set_search".$i." ' action='settings.php' method='get'><p><label for='q1'>name</label><input type='text' id='q' name='q'/></p><p><label for='q2'>uniqid</label>
<input type='text' id='q2' name='q2' /></p><p><input type='hidden' id='q3' name='q3' value='1' /></p><p><input type='submit' value='Search'/></p></form></div></p>";
}
?>
</body>
</html>
$(文档).ready(函数(){
$('form[id*=“set_search”]')。验证({
规则:{
问:{
要求:正确,
最小长度:2
},
问题2:{
电子邮件:真的
},
},
信息:{
q1:“请输入您的姓名”,
问题2:“请输入有效的电子邮件地址”,
}
});
});
.错误{
颜色:红色;
字体:12pt verdana;
左侧填充:10px
}
.输入{
边缘顶部:10px;
左边距:20px;
}
这段代码应该可以工作,但有些不好:我用当前代码发布了一个答案。请看一看,也许你看到了什么问题。我更改了$(“输入[id^='set_搜索])))x$(“表单[id^='set_搜索]”)