Php表单验证,Post
这是我表格的标题Php表单验证,Post,php,html,forms,footer,Php,Html,Forms,Footer,这是我表格的标题 <form action="/page" id="landing_form" method="post" style="top:<?php echo get_post_meta($p->ID, 'top', true); ?>px; left:<?php echo get_post_meta($p->ID, 'left', true); ?>px;" > 我的表单中有一个简单的文本字段和提交按钮 <label>Em
<form action="/page" id="landing_form" method="post" style="top:<?php echo get_post_meta($p->ID, 'top', true); ?>px; left:<?php echo get_post_meta($p->ID, 'left', true); ?>px;" >
我的表单中有一个简单的文本字段和提交按钮
<label>Email</label><input type="text" value="" id="landing_email" name="email" >
<input type="submit" name="submit" value="<?php echo get_post_meta($p->ID, 'button', true); ?>" class="landing_submit" id="landing_submit">
现在,我该如何创建一个函数来检查电子邮件中是否有一个。/?或者是一个特定的字符串
我试着在我的标题中添加类似onSubmit=returnfunction'email的内容;然后将函数放入文件中
但是,它甚至不检查函数,只提交表单,不管结果如何。假设您需要客户端验证,您必须创建如下所示的javascript函数 因此,您需要您的方法:
<script>
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
function validate(email) {
if (!validateEmail(email)) {
return false;
}
}
</script>
如果电子邮件无效,则不允许提交表单。
请注意,您还必须在服务器端验证电子邮件。当按下提交按钮时,onSubmit属性调用一段javascript。它根本不与PHP交互
您可能希望在页面中看到以下内容:
<?php
if(isset($_POST['submit']))
{
$email = $_POST['email'];
if(strpos($email, '@') && strpos($email, '.') && strpos($email, 'some specific string'))
{
// This code executes
// only if there is an @
// and a full stop and "some specific string"
// inside the email
}
}
?>
是一个函数,用于在字符串中搜索子字符串并返回其第一次出现的位置。如果找不到子字符串,则返回false。如果我理解正确,您实际上希望使用JavaScript进行客户端验证。e、 g.在数据满足您的需要之前,您不希望表单提交到服务器。这并不排除服务器端验证需要。。。但是你现在想要的是基于浏览器的验证-对吗?现在有没有办法退出帖子并返回表单?或者它会完全停止。更多的是预检查,我不确定这是否可以用作预检查,或者只有在帖子通过后才能使用。你可以在表单所在的页面上设置此选项,以便它返回表单并在无效时显示消息。我会检查此选项。谢谢
<?php
if(isset($_POST['submit']))
{
$email = $_POST['email'];
if(strpos($email, '@') && strpos($email, '.') && strpos($email, 'some specific string'))
{
// This code executes
// only if there is an @
// and a full stop and "some specific string"
// inside the email
}
}
?>