简体   繁体   English

使用php注册表格中的javascript验证电话号码的问题

[英]Issue in validate phone number using javascript in php register form

Here is my code for validating phone number; 这是我验证电话号码的代码;

  if(!(preg_match([0-9]{10})|(\([0-9]{3}\)\s+[0-9]{3}\-[0-9]{4}, $_POST['phone'])))
        die(msg(0,"You haven't provided a valid phone number"));

After enter all the input details, it shows like loading. 输入所有输入详细信息后,它显示为正在加载。

Can anyone say what is the mistake in my code. 谁能说我代码中的错误是什么。

Thanks in advance. 提前致谢。

Change your code to: 将您的代码更改为:

if(!(preg_match("/([0-9]{10})|([0-9]{3}\\s+[0-9]{3}\\s+[0-9]{4})/", $_POST['phone'])))
die(msg(0,"You haven't provided a valid phone number"));

Mainly, there were two problems with your regex. 正则表达式主要有两个问题。

First: You had forgot to put your regular expression inside quotes. 第一:您忘记将正则表达式放在引号中。 Second: After the opening quote, and before the closing quote, you should have / 第二:在开始报价之后和结束报价之前,您应该有/

This building the pregmatch like: preg_match("/regex/", $string); 这样就建立了pregmatch,例如:preg_match(“ / regex /”,$ string);

And finally, the regular expression itself contained some errors. 最后,正则表达式本身包含一些错误。

I recommend using this tool if you are new to regex: 如果您是正则表达式的新手,我建议您使用此工具:

http://regex101.com/ http://regex101.com/

Second part: 第二部分:

The reason you are having issues with your form showing loading is because the PHP-script which it is posting data to, is throwing an error. 您的表单显示加载出现问题的原因是因为它向其发布数据的PHP脚本抛出错误。 This error needs to be fetched by the ajax script and returned to the browser. 该错误需要由ajax脚本获取并返回到浏览器。 Simply using die('a message'); 只需使用die('a message'); will kill the script and return "a message" to the query. 将杀死脚本并向查询返回“一条消息”。 You need to add a function to handle any error responses. 您需要添加一个函数来处理任何错误响应。

Use a browser developer tool like FireBug or any buil in tool in your browser to have a look at the output of your ajax query: 使用FireBug之类的浏览器开发人员工具或浏览器中的任何内置工具来查看ajax查询的输出:

在此处输入图片说明

A good way to do a ajax call would be like: 进行ajax调用的一种好方法是:

var formData = $('#yourform').serialize();
$.ajax({
    url : "/link_to_script.php",
    type : "POST",
    data: formData,
    dataType: "json",
    success : function(d){
        if( d.success) {
            // An array key called success is located with the value of true
            alert( 'Successfully submitted form' );
        }
        if( d.error ){
            // An array key called error is located with the value of true
            alert( d.errormsg );
            // Alerts the visitor with the content of the return array key "errormsg"
        }
    },
    error: function(){
        alert( 'Something went wrong' );
        // This error function will be returned if the data returned by script is not json, or if the script cannot be reached / returns any kind of 404/500 error or such.
    }
});

This will handle a json output from your PHP script and interperate its data. 这将处理PHP脚本的json输出并对其数据进行互操作。 If the call is successfull, it should contain some data from your PHP script, like for example a success message or an error message. 如果调用成功,则应包含PHP脚本中的一些数据,例如成功消息或错误消息。 This needs to be return like for example this: 这需要返回,例如:

<?php
    echo json_encode( array( 'success'=> true, 'message' => 'Uploaded file' ) );
    // OR, incase error like the preg_match:
    echo json_encode( array( 'error' => true, 'errormsg' => 'Phone not validated' ) );

This will json encode an array with the data you want to handle with your ajax script. 这将对要使用ajax脚本处理的数据进行json编码。

您可以使用以下代码在浏览器中验证手机号码。

<input name="mobilenum" pattern="[789][0-9]{9}" placeholder="Contact Number" required="" type="text">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM