简体   繁体   English

acrobat表单字段验证正则表达式

[英]acrobat form field validation regular expressions

Working in Acrobat X and trying to validate a field using a Regular Expression. 在Acrobat X中工作,并尝试使用正则表达式验证字段。 My problem is that when I use the following in my JavaScript code: 我的问题是,当我在JavaScript代码中使用以下代码时:

var myRegExp = /dog/;
var myTextInput = event.value;

event.rc = true;

if (myTextInput != myRegExp) {
    app.alert("It's a regex why do I need to input '/dog/'! and not just dog");
} 

entered within the Variable tab of the Text Field Properties dialogue box - it does not see it as a Regular Expression. 在“文本字段属性”对话框的“变量”选项卡中输入-不会将其视为正则表达式。

That is to say; 也就是说; if I type 'dog' (ignore quote marks) into the live pdf field it gives an error, but if I input '/dog/' (ignore quote marks) into the field it works fine. 如果我在实时pdf字段中输入“ dog”(忽略引号),则会出现错误,但如果我在字段中输入“ / dog /”(忽略引号),则效果很好。

My Question: why is my var not being recognized as a Regular Expression. 我的问题:为什么我的var不被识别为正则表达式。

I had read that the open '/' and close '/' (ignore quote marks) defined a Regular Expression but this does not appear to be case. 我已经读过,打开'/'和关闭'/'(忽略引号)定义了一个正则表达式,但事实并非如此。 What am I missing here? 我在这里想念什么?

The issue seems to be that you're testing for equality between a RegExp object and a string. 问题似乎是您正在测试RegExp对象和字符串之间的相等性。 Instead, you need to use the test method: 相反,您需要使用test方法:

var myRegExp = /dog/;
var myTextInput = event.value;

event.rc = myRegExp.test(myTextInput);

See MDN for more documentation . 有关更多文档,请参见MDN

The following operation is incorrect and will always return false : 以下操作不正确,并且始终返回false

/dog/ == "dog";

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

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