I have written ng-pattern for not to start and end with space. And trying to match double quotation in $watch.
But it is not working. ng-pattern is also not working
my ng-pattern is:
<input type="text" class="form-control" ng-model="wifiResult.ssid"
name="ssid"
ng-class="{'inputError' : errorMsg}"
ng-minlength="1"
ng-maxlength="32"
ng-pattern="/^[^\s]+(\s+[^\s]+)*$/"
required >
$watch in JS is:
$scope.$watch('wifiResult.ssid', function(scope){
var regexToRestrict = /^["]+$/;
if(scope){
var pressChar = scope.slice(-1);
if(pressChar.match(regexToRestrict)){
scope = scope.slice(0,-1); // try to replace " to blank
}
else{
console.log('not matched');
}
}
else{
console.log('no scope');
}
});
Is there any other way to do this?
Thanks in advance !
You can change your regex to like this ng-pattern='/^[^\\\\./:*?\"<>|][^\\\\/:*?\"<>|]{0,254}$/'
in your html to prevent user from entering (") in input and form can throw an error saying it's an invalid input.
In angular ng-trim
is by default set to true so any white spaces surrounding user input are removed before assigning to ng-model
.
You don't need a watcher unless and until you want the input value to be (") free every time user tries to enter some value in input field.It's an unnecessary operation to validate every time user changes something.