输入php时验证文本框 [英] validate textbox while typing php

查看:163
本文介绍了输入php时验证文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有文本输入字段的表单。

 < form method =postaction =这个文本输入需要在用户输入数据时同时验证。通讯/ tester.php> 
< label for =email>电子邮件< / label>
< input type =textname =emailplaceholder =输入您的电子邮件地址*maxlength =50required>
< input type =submitvalue =test>
< / form>

输入电子邮件文本框应经过验证服务器端,但我没有任何关于在输入时验证文本字段的想法。



有没有办法使用php脚本来完成它?

解决方案

如果你愿意使用jQuery和PHP,下面是你可以做的一个例子:

HTML

 < form action =Newsletter / tester.php方法= POST > 
< input type =emailname =emailid =email-input>
< button type =submit>发送< / button>
< br />
< span id =email-validate>等待用户输入...< / span>
< / form>

jQuery
< pre $ jQuery(function($){

//当用户在输入
$('#email-input')中输入内容时。 on('keyup',function(e){

//设置变量
var $ input = $(this)
,value = $ input.val()
,data = {
email:value
};

//执行ajax调用
$ .ajax({
url:' my / validator.php',//这是目标
方法:'get',// method
data:data,//将输入值传递给服务器
success:function( r){//如果http响应代码是200
$('#email-validate').css('color','green')。html(r);
$('button ').removeAttr('disabled');
},
error:function(r){//如果http响应代码不是200
$('#email-validate').css('color','red')。html(r);
$('button')。attr('disabled','disabled');
}

});
});

});

PHP (> = 5.4)

 <?php 
if(isset($ _ GET ['email'])){
$ email = $ _GET ['email ];

if(filter_var($ email,FILTER_VALIDATE_EMAIL)){
http_response_code(200);
echo'The email'。$ email。'is valid';
} else {
http_response_code(412);
echo'The email'。$ email。'无效';
}
}


I am having a form with a text input field. The text input need to be validated simultaneously when the user enters the data.

<form method="post" action="Newsletter/tester.php">
    <label for="email">email</label>
    <input type="text" name="email" placeholder="Enter your email  *" maxlength="50" required>
    <input type="submit" value="test">
</form>

The input email textbox should be validated on the server side, but I don't have any idea about validating the text field while typing.

Is there any way to do it using php script?

解决方案

If you are willing to use jQuery and PHP, here's an exemple of what you can do:

HTML

<form action="Newsletter/tester.php" method="post">
    <input type="email" name="email" id="email-input">
    <button type="submit">Send</button>
    <br/>
    <span id="email-validate">Waiting for user input...</span>
</form>

jQuery

jQuery(function($) {

    // When the user types something in the input
    $('#email-input').on('keyup', function(e) {

        // set the variables
        var $input = $(this)
        ,   value  = $input.val()
        ,   data   = {
                email: value   
        };

        // perform an ajax call
        $.ajax({
            url: 'my/validator.php', // this is the target
            method: 'get', // method
            data: data, // pass the input value to server
            success: function(r) { // if the http response code is 200
                $('#email-validate').css('color', 'green').html(r);
                $('button').removeAttr('disabled');
            },
            error: function(r) { // if the http response code is other than 200
                $('#email-validate').css('color', 'red').html(r);
                $('button').attr('disabled', 'disabled');
            }

        });
    });

});

PHP (>= 5.4)

<?php
if(isset($_GET['email'])) {
    $email = $_GET['email'];

    if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
        http_response_code(200);
        echo 'The email '.$email.' is valid';
    } else {
        http_response_code(412);
        echo 'The email '.$email.' is not valid';
    }
}

这篇关于输入php时验证文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆