Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 验证加拿大邮政编码Regex_Php_Javascript_Mysql_Regex - Fatal编程技术网

Php 验证加拿大邮政编码Regex

Php 验证加拿大邮政编码Regex,php,javascript,mysql,regex,Php,Javascript,Mysql,Regex,我有一个用JavaScript编写的脚本,可以使用Regex验证加拿大邮政编码,但它似乎不起作用。以下是脚本: 如果声明: if (myform.zip.value == "" || myform.zip.value == null || myform.zip.value == "Postal Code" || myform.zip.value.length < 12 ) { alert("Please fill in field Postal Code. You should o

我有一个用JavaScript编写的脚本,可以使用Regex验证加拿大邮政编码,但它似乎不起作用。以下是脚本:

如果声明:

if (myform.zip.value == "" || myform.zip.value == null || myform.zip.value == "Postal Code" || myform.zip.value.length < 12 ) {
    alert("Please fill in field Postal Code. You should only enter 7 characters");
    myform.zip.focus();
    return false;
  }

正则表达式方法可以验证,但不足以保证邮政编码实际存在

例如:
A9A 0A0
看起来像一个有效的加拿大邮政编码,但正向分拣区
A9A


您确定不想对官方邮政编码列表进行某种查找吗?

第一个错误是初始if语句中的最后一个条件
myform.zip.value.length<12
应始终为真,因此这部分代码将始终提示消息
“请填写字段邮政编码。您只需输入7个字符”
,然后将焦点放回
zip
字段。由于有效邮政编码最多有7个字符,因此应将其更改为
myform.zip.value.length>7

更正后,您在注释中提供的邮政编码
T2X 1V4
将生效。但是,可以简化您使用的正则表达式(注释中也提到了这一点)。您可以删除
{1}
的所有实例,因为它们是冗余的。您可能还想在空格后面加一个
而不是
*
表示前面的字符或表达式可以出现0或1次,而
*
表示它可以出现0或更多次。我想你的邮政编码最多只需要一个空格

以下是我测试此功能的完整工作代码:

<!doctype html>
<html>
<head>
    <title>JavaScript Regex Tester</title>
    <meta charset="utf-8">
    <script>
        function validate(myform) {
            if (myform.zip.value == "" || myform.zip.value == null || myform.zip.value == "Postal Code" || myform.zip.value.length > 7 ) {
                alert("Please fill in field Postal Code. You should only enter 7 characters");
                myform.zip.focus();
                return false;
            }

            return okNumber(myform);
        }

        function okNumber(myform) {
            var regex = /^[ABCEGHJKLMNPRSTVXY]\d[A-Z] *\d[A-Z]\d$/;
            if (regex.test(myform.zip.value) == false) {
                alert("Input Valid Postal Code");
                myform.zip.focus();
                return false;
            }

            return true;
        }
    </script>
</head>
<body>
    <form action="#" name="myform" method="post">
        <input type="text" name="zip" value="Postal Code" />
        <input type="button" value="Submit" onclick="validate(document.myform);"/>
    </form>
</body>
</html>

JavaScript正则表达式测试器
函数验证(myform){
如果(myform.zip.value==“”| | myform.zip.value==null | | | myform.zip.value==“邮政编码”| | myform.zip.value.length>7){
警告(“请填写字段邮政编码。您只能输入7个字符”);
myform.zip.focus();
返回false;
}
返回单号(myform);
}
函数编号(myform){
var regex=/^[ABCEGHJKLMNPRSTVXY]\d[A-Z]*\d[A-Z]\d$/;
if(regex.test(myform.zip.value)==false){
警报(“输入有效邮政编码”);
myform.zip.focus();
返回false;
}
返回true;
}

最后一个注意事项是,通常当您在正则表达式中看到
[A-Z]
时,至少需要考虑它是否应该是
[A-Za-Z]
以接受大写或小写字母。我不知道加拿大邮政编码是否如此,但通常情况下,大多数表单都应接受输入,并根据需要更正大小写。

这适用于所有加拿大邮政编码

^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$
这里是规则


ABCEGHJKLMNPRSTVXY这是我使用的加拿大邮政编码的有效表达式。
它的格式将严格为A0A 0A0

/^[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$/

这适用于加拿大邮政编码:

/^[a-z][0-9][a-z][- ]?[0-9][a-z][0-9]$/i

它将允许正确的X#X#X#格式,并带有空格或连字符。

解决方案对于您的回答,我希望能有所帮助。谢谢你的时间

string[] inputName = new string[5];
    string[] inputId = new string[5];
    string[] inputMarks = new string[5];

    Regex StudentId = new Regex(@"\d\d\d\d\d$");
    Regex Marks = new Regex(@"\d\d$");
    Regex StudentName = new Regex(@"^([a-zA-z\s]{5,10})$");

    private void btnClear_Click(object sender, EventArgs e)
    {
        rtShowAll.Clear();

    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        string Name = txtLastName.Text;
        string id = txtStudentId.Text;
        string marks = txtMarks.Text;

        if ((Name == "") || (!StudentName.IsMatch(Name)))
        {
            MessageBox.Show("space cannot be empty and enter valid characters only");
        }

        else if (Name != "")
        {
            if ((id == null) || (StudentId.IsMatch(id)))
            {
                MessageBox.Show("Enter valid id");
            }
            else if ((id != null) || (StudentId.IsMatch(id)))
            {
                if ((marks == null) || (!Marks.IsMatch(marks)))
                {
                    MessageBox.Show("enter valid marks");
                }

                else if ((marks != null) || (Marks.IsMatch(marks)))
                {
                    for (int i = 0; i <= 5; i++)
                    {
                        inputName[i] = Name;
                        inputId[i] = id;
                        inputMarks[i] = marks;
                        break;
                    }
                }

            }
        }
        txtLastName.Clear();
        txtMarks.Clear();
        txtStudentId.Clear();

    }

    private void btnShowAll_Click(object sender, EventArgs e)
    {


        string result = "";
        for (int i = 0; i <= 5; i++)
        {
            result = inputName[i] + "   " + inputId[i] + "   " + inputMarks[i];
            rtShowAll.Text = result;
            break;
        }

       //list.Add(rtShowAll.Text);
        //string Showall = "";
       // foreach (string s in list)
      // {
       //    Showall += s + " "+ "\n";
        //}
       // rtShowAll.Text = Showall;
    }

    private void btnSearch_Click(object sender, EventArgs e)
    {
        string searchId = txtStudentId.Text;
        string result = "";

        txtStudentId.Text = searchId;

        for (int i = 0; i < 5; i++)
        {
            if (searchId == inputId[i])
            {
                result = inputName[i] + "    " + inputMarks[i];
                rtSearch.Text = result;
            }
            else if (searchId != inputId[i])
            {
                MessageBox.Show("Enter valid Student id");
            }
        }
    }

    private void btnModify_Click(object sender, EventArgs e)
    {
        string id = txtStudentId.Text;
        string newmarks = "";

        for (int i = 0; i < 5; i++)
        {
            if (id == inputId[i])
            {
                newmarks = txtMarks.Text;
                if ((newmarks == null) || (!Marks.IsMatch(newmarks)))
                {
                    MessageBox.Show("enter valid marks");
                }
                else if ((newmarks != null || (Marks.IsMatch(newmarks))))
                {
                    inputMarks[i] = newmarks;
                    MessageBox.Show("marks has been modified");
                }
            }
        }
    }
}
string[]inputName=新字符串[5];
字符串[]inputId=新字符串[5];
字符串[]inputMarks=新字符串[5];
Regex StudentId=new Regex(@“\d\d\d\d$”);
正则表达式标记=新正则表达式(@“\d\d$”);
Regex StudentName=newregex(@“^([a-zA-z\s]{5,10})$”;
私有void btnClear\u单击(对象发送者,事件参数e)
{
rtShowAll.Clear();
}
私有void btnAdd_单击(对象发送者,事件参数e)
{
字符串名称=txtLastName.Text;
字符串id=txtStudentId.Text;
字符串标记=txtMarks.Text;
如果((Name==“”)| |(!StudentName.IsMatch(Name)))
{
MessageBox.Show(“空格不能为空,只能输入有效字符”);
}
else if(名称!=“”)
{
if((id==null)| |(StudentId.IsMatch(id)))
{
MessageBox.Show(“输入有效id”);
}
else如果((id!=null)| |(StudentId.IsMatch(id)))
{
如果((marks==null)| |(!marks.IsMatch(marks)))
{
MessageBox.Show(“输入有效标记”);
}
else如果((marks!=null)| |(marks.IsMatch(marks)))
{

对于(int i=0;i,
{1}
业务是毫无意义的。一个
[]
字符类已经是一个隐式
{1}
总之,与
\d
和任何其他匹配单个字符的东西一样。除此之外,这是怎么不起作用的?您是否检查了输入没有前导/尾随空格?输入和输出的示例是什么?或者它根本不起作用有效的加拿大邮政编码的规则是什么?什么测试输入不起作用regex失败了吗?根本不起作用。一个有效的邮政编码示例是T2X 1V4。我看不到在第一个代码块的任何地方调用okNumber…这段代码正在执行吗?在这种情况下,类似于
^[A-Z]\d[A-Z][?\d[A-Z]\d$
。如果这个regex在您的程序中不起作用,您可能会有一个逻辑错误(true而不是false?),可能是错误地调用了正则表达式代码,或者根本没有调用它。请解释您为OP编写的代码
/^[a-z][0-9][a-z][- ]?[0-9][a-z][0-9]$/i
string[] inputName = new string[5];
    string[] inputId = new string[5];
    string[] inputMarks = new string[5];

    Regex StudentId = new Regex(@"\d\d\d\d\d$");
    Regex Marks = new Regex(@"\d\d$");
    Regex StudentName = new Regex(@"^([a-zA-z\s]{5,10})$");

    private void btnClear_Click(object sender, EventArgs e)
    {
        rtShowAll.Clear();

    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        string Name = txtLastName.Text;
        string id = txtStudentId.Text;
        string marks = txtMarks.Text;

        if ((Name == "") || (!StudentName.IsMatch(Name)))
        {
            MessageBox.Show("space cannot be empty and enter valid characters only");
        }

        else if (Name != "")
        {
            if ((id == null) || (StudentId.IsMatch(id)))
            {
                MessageBox.Show("Enter valid id");
            }
            else if ((id != null) || (StudentId.IsMatch(id)))
            {
                if ((marks == null) || (!Marks.IsMatch(marks)))
                {
                    MessageBox.Show("enter valid marks");
                }

                else if ((marks != null) || (Marks.IsMatch(marks)))
                {
                    for (int i = 0; i <= 5; i++)
                    {
                        inputName[i] = Name;
                        inputId[i] = id;
                        inputMarks[i] = marks;
                        break;
                    }
                }

            }
        }
        txtLastName.Clear();
        txtMarks.Clear();
        txtStudentId.Clear();

    }

    private void btnShowAll_Click(object sender, EventArgs e)
    {


        string result = "";
        for (int i = 0; i <= 5; i++)
        {
            result = inputName[i] + "   " + inputId[i] + "   " + inputMarks[i];
            rtShowAll.Text = result;
            break;
        }

       //list.Add(rtShowAll.Text);
        //string Showall = "";
       // foreach (string s in list)
      // {
       //    Showall += s + " "+ "\n";
        //}
       // rtShowAll.Text = Showall;
    }

    private void btnSearch_Click(object sender, EventArgs e)
    {
        string searchId = txtStudentId.Text;
        string result = "";

        txtStudentId.Text = searchId;

        for (int i = 0; i < 5; i++)
        {
            if (searchId == inputId[i])
            {
                result = inputName[i] + "    " + inputMarks[i];
                rtSearch.Text = result;
            }
            else if (searchId != inputId[i])
            {
                MessageBox.Show("Enter valid Student id");
            }
        }
    }

    private void btnModify_Click(object sender, EventArgs e)
    {
        string id = txtStudentId.Text;
        string newmarks = "";

        for (int i = 0; i < 5; i++)
        {
            if (id == inputId[i])
            {
                newmarks = txtMarks.Text;
                if ((newmarks == null) || (!Marks.IsMatch(newmarks)))
                {
                    MessageBox.Show("enter valid marks");
                }
                else if ((newmarks != null || (Marks.IsMatch(newmarks))))
                {
                    inputMarks[i] = newmarks;
                    MessageBox.Show("marks has been modified");
                }
            }
        }
    }
}