PHP URL表单验证

时间:2016-04-15 01:28:41

标签: php jquery forms twitter-bootstrap validation

我有一个我正在尝试构建的表单。基本上我正在使用一个已经内置了php验证的表单的基本引导程序模板。内置,它专门验证电子邮件地址。但是我试图在另一个字段中添加一个有效的URL并让php验证这个其他字段以传递给一封电子邮件。

目前我的代码在html中是这样的:

curl -v 'http://jsnice.org/beautify?pretty=1&rename=1&types=1&suggest=0&language_in=ecmascript6' -d @output.js

我的php连接到这个如下:

<div class="row">
        <div class="col-md-8">
            <h3>Order/Estimate Form</h3>
            <p style="color:red;">*Please Fill In All Fields*</p>
            <form name="sentMessage" id="contactForm" novalidate>
                <div class="control-group form-group">
                    <div class="controls">
                        <label>Full Name:</label><p style="color:gray;">* required field</p>
                        <input type="text" class="form-control" id="name" required data-validation-required-message="Please enter your name.">
                        <p class="help-block"></p>
                    </div>
                </div>
                <div class="control-group form-group">
                    <div class="controls">
                        <label>Company</label>
                        <input type="company" class="form-control" id="company">
                    </div>
                </div>
                <div class="control-group form-group">
                    <div class="controls">
                        <label>Phone Number:</label><p style="color:gray;">* required field</p>
                        <input type="tel" class="form-control" id="phone" required data-validation-required-message="Please enter your phone number.">
                    </div>
                </div>
                <div class="control-group form-group">
                    <div class="controls">
                        <label>Email Address:</label><p style="color:gray;">* required field</p>
                        <input type="email" class="form-control" id="email" required data-validation-required-message="Please enter your email address.">
                    </div>
                </div>
                <div class="control-group form-group">
                    <div class="controls">
                        <label>Download Link</label><p style="color:gray;">* required field</p>
                        <input type="url" class="form-control" id="url" required data-validation-required-message="Please enter a valid shareable download link from wetransfer.com">
                    </div>
                </div>
                <div class="control-group form-group">
                    <div class="controls">
                        <label>Subject:</label><p style="color:gray;">* required field</p>
                        <input type="subject" class="form-control" id="subject" required data-validation-required-message="Please enter a subject.">
                    </div>
                </div>

                <div class="control-group form-group">
                    <div class="controls">
                        <label>Message:</label><p style="color:gray;">* required field</p>
                        <textarea rows="10" cols="100" class="form-control" id="message" required data-validation-required-message="Please enter your message" maxlength="999" style="resize:none"></textarea>
                    </div>
                </div>
                <div id="success"></div>
                <!-- For success/fail messages -->
                <button type="submit" class="btn btn-primary">Send Message</button>
            </form>
        </div>

    </div>

此外,这是jQuery验证代码:

<?php
// check if fields passed are empty
if(empty($_POST['name'])        ||
   empty($_POST['phone'])       ||
   empty($_POST['subject'])   ||
   empty($_POST['email'])       ||
   empty($_POST['url'])   ||
   empty($_POST['message']) ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   !filter_var($_POST['url'],FILTER_VALIDATE_URL))
   {
    echo "No arguments Provided!";
return false;
   }

$name = $_POST['name'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$url = $_POST['url'];
$email_address = $_POST['email'];
$message = $_POST['message'];

// create email body and send it    
$to = 'mail@email.com'; // PUT YOUR EMAIL ADDRESS HERE
$email_subject = "Quote/Pricing Form:  $name"; // EDIT THE EMAIL SUBJECT LINE HERE
$email_body = "You have received a new message from your website's contact form.\n\n"."Here are the details:\n\nName: $name\n\nPhone: $phone\n\nEmail: $email_address\n\nURL: $url\n\nSubject: $subject\n\nMessage:\n$message";
$headers = "From: noreply@email.com\n";
$headers .= "Reply-To: $email_address"; 
mail($to,$email_subject,$email_body,$headers);
return true;            
?>

1 个答案:

答案 0 :(得分:0)

您想使用FILTER_VALIDATE_URL

如果你想首先使用jquery来验证这个url,那么你只需要一个调用url到另一个php文件的调用,一旦成功,你就可以执行其余的jquery代码。