我想在JavaScript中创建一个String.replaceAll()方法,我认为使用RegEx将是最简洁的方法.但是,我无法弄清楚如何将变量传递给RegEx.我已经可以做到这一点,它将用"A"替换所有"B"的实例.
"ABABAB".replace(/B/g, "A");
Run Code Online (Sandbox Code Playgroud)
但我想做这样的事情:
String.prototype.replaceAll = function(replaceThis, withThis) {
this.replace(/replaceThis/g, withThis);
};
Run Code Online (Sandbox Code Playgroud)
但显然这只会替换文本"replaceThis"...所以如何将此变量传递给我的RegEx字符串?
我有一个带有输入字段和验证设置的表单,通过添加required属性等.但对于某些领域,我需要做一些额外的验证.我如何"点击" FormController控件的验证?
自定义验证可能类似于"如果填写了这3个字段,则此字段是必需的,需要以特定方式进行格式化".
有一种方法,FormController.$setValidity但看起来不像公共API,所以我宁愿不使用它.创建一个自定义指令并使用NgModelController看起来像另一个选项,但基本上要求我为每个自定义验证规则创建一个指令,这是我不想要的.
实际上,将控制器中的字段标记为无效(同时保持FormController同步)可能是我在最简单的场景中完成工作所需要的,但我不知道该怎么做.
我有一个表单,如果复选框为false,则使用ng-required指令对文本输入强制执行验证.如果复选框为true,则隐藏该字段,并将ng-required设置为false.
问题是我还在输入上指定了验证的正则表达式,并使用了ng-pattern angular指令.我遇到的问题是,如果用户填写了无效的电话号码,请选中该框以停用该输入(因此无需进一步验证),表单将不允许提交,因为它基于ng-pattern无效.
我尝试通过添加ng-change函数来将输入模型设置为null来解决此问题,但是ng-pattern和字段仍然在复选框的初始设置为false时设置为无效.但是,如果我取消选中该框,将所有内容设置回初始表单加载,然后再次选中该框,表单有效并且能够提交.我不确定我错过了什么.这是我到目前为止的ng-change代码:
var phoneNumberRegex = /^\(?(\d{3})\)?[ .-]?(\d{3})[ .-]?(\d{4})$/;
$scope.phoneNumberPattern = phoneNumberRegex;
$scope.removeValidation = function() {
if ($scope.cell._newUser === false) {
$scope.request._number = '';
$scope.phoneNumberPattern = /[0-9a-zA-Z]?/;
} else {
$scope.phoneNumberPattern = phoneNumberRegex;
}
};
Run Code Online (Sandbox Code Playgroud) 我试图使用ng-pattern指令验证angularJs中的电子邮件ID字段.
但我是AngularJs的新手.一旦用户输入错误的电子邮件ID,我就需要显示错误消息.
我下面的代码是试图解决的.帮助我使用ng-pattern来获得正确的结果.
<script type="text/javascript" src="/Login/script/ang.js"></script>
<script type="text/javascript">
function Ctrl($scope) {
$scope.text = 'enter email';
$scope.word = /^[a-z]+[a-z0-9._]+@[a-z]+\.[a-z.]{2,5}$/;
}
</script>
</head>
<body>
<form name="myform" ng-controller="Ctrl">
<input type="text" ng-pattern="word" name="email">
<span class="error" ng-show="myform.email.$error.pattern">
invalid email!
</span>
<input type="submit" value="submit">
</form>
</body>
Run Code Online (Sandbox Code Playgroud) 我有一个带有密码和确认密码字段的表单,我想验证他们没有额外的按钮就得到了相同的值.
这是我的代码:
<input type="password" id="password" name="password" ng-model="password" placeholder='Password'
ng-pattern="/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{6,20}/";
ng-blur="focused4 = false" ng-focus="focused4 = true"
ng-minlength="6" ng-maxlength="20" required>
<span ng-show="focused4 && !regForm.password.$valid"><error>6 to 20 characters, one numeric digit, one upper case letter, one lower case letter and a special character</error></span>
</td>
<td align="left">
<span ng-show="regForm.password.$valid"><valid>✔</valid></span>
<span ng-show="!regForm.password.$valid"><error>❌</error></span>
</td>
</tr>
<tr>
<td align="left">
<input type="password" id="passwordConfirm" name="passwordConfirm" ng-model="passwordConfirm" placeholder="Confirm Password"
ng-pattern="/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{6,20}/";
ng-blur="focused5 = false" ng-focus="focused5 = true"
ng-minlength="6" ng-maxlength="20" required>
<span ng-show="focused5 && !regForm.passwordConfirm.$valid" ><error>Passwords are not correct or don't match</error></span> …Run Code Online (Sandbox Code Playgroud)