Validation on text on alphabets enter - angularjs

Any one use AngulerJS Validation on TextBox. So that only enter alphabets.

Depending on what you want:
Alphabets and blankspace:
ng-pattern="/^[a-zA-Z\s]*$/"
Alphabets no blankspace:
ng-pattern="/^[a-zA-Z]*$/"

I guess all the other answers using ng-pattern are correct but just to mention, the use of ng-model is compulsory with ng-pattern.
The complete input can be:
<input type="text" ng-pattern ="/^[a-zA-Z\s]*$/" ng-model="Name" />
This will accept the alphabets and the spaces only.

add this to your input
ng-pattern="/^[a-zA-Z]$/"

If you don't want to use ng-pattern for whateever reason, you can also add validating functions to the ng-modal-controller that is set up on your textbox, specifically to the formatters and parsers arrays. A good description of how to do this is here: http://www.benlesh.com/2012/12/angular-js-custom-validation-via.html. Another alternative is to use a published directive to achieve the same purpose, I haven't used this one myself but it seems legit: http://www.brentmckendrick.com/code/xtform/

There is a directive called ng-pattern it allows to use regular expressions to indicate which are the allowed values.
It can be used like this:
<input ng-pattern="/[a-zA-Z]/" ...

Related

Which locator to use for this piece of code which is input field

I have a question, there is another input field which I do not understand how to use locator for. In the code it is
'<input _ngcontent-tvt-19="" class="form-control ng-pristine ng-valid ng-touched" placeholder="You can search keywords" type="text">
I want to fill this field with a value using the code in spec file as
'element(by.binding('You can search keywords ')).sendKeys("04");
But it does not work. I have also tried with cssContainingText and it did not work. Do you have any idea about the locator or how can I change the code line.
You can use by using xpath, as below it will work
element(by.xpath("//input[#placeholder='You can search keywords']"));
Easiest solution would be to add an ID or a class to your input field and then use:
element(by.Id(<id>))
or :
element(by.css(<class_name>))
Here are a couple of examples:
$('input[placeholder="You can search keywords"]')
$('input[_ngcontent-tvt-19]')

ng-pattern allow only letters, numbers, dot, underscore and dash

How'd allow for an input using ng-pattern only: letters, numbers, dot, underscore and dash ( . _ -)?
Already tried the following ones
UPDATE:
$scope.validationPattern = '/^[a-zA-Z\d\-\_]+$/';
<input ng-model="model" type="text" ng-pattern="{{validationPattern}}" />
Judging by your requirements, you need to add a dot to the pattern:
$scope.regex = '^[a-zA-Z0-9._-]+$';
and add ng-trim="false" to the <input> tag so as to disallow leading/trailing spaces.
See this plunkr
[a-zA-Z0-9][A-Za-z0-9._-]*
this one works
/^[a-zA-Z0-9 ._-]+$/
This regex seems appropriate enough for you if it don't directly fit in ng-pattern make necessary adjustments, like.
in controller add
$scope.pattern = /^[a-zA-Z0-9 ._-]+$/;
now in html use ng-pattern="pattern"
Tested using this : http://www.regextester.com/
Accepts numbers, alphabets and three symbols(.-_)

Starting with Number and Ending with Character in Angular JS

I just started learning AngularJs So sorry for dump question. I have requirement to validate the field which will start with number and end with 'M' character eg:- 10M, 5M, 200M etc. please any one help me how to do it in angular js.
You can use ng-pattern in your input field e.g.
<input type="text" ng-model="model" id="input" name="input" ng-pattern="regex" />
and in your controller define regex as
$scope.regex = '\\d+M';
And you can check in your html, if input is valid or not like this:
input valid? = <code>{{form.input.$valid}}</code>
This will work well with ng-messages directive for form validation.
Read More.
You can use the ngPattern directive to validate by matching a regular expression:
Angular docs: ngPattern directive
The pattern you need is \d+M (a sequence of 1..n digits, followed by an 'M'), you can try it out on the page I linked.
AngularJS provide a usefull directive ng-pattern that you can apply on an input.
For your problem, you can use this code :
<input type="text" ng-pattern="/^\d+[M]$/" ng-model="model" id="input" />
Explanation
/d means digits only
+ means at least one
[M] means M characters only

How to check the given text doesnot contain full zero in ng-pattern?

i need to check the given the input text doesnot contain full zeors in ng-pattern.
for eg my I/p is :00002210000 it should accept if my I/p is:0000000000 it should not accept it should throw an error.
Use javascript match().
if (!myString.match(/(0*[1-9]+0*)+/) {
alert("Invalid string!");
};
The above requires the input to have at least one non-zero number. Here's a fiddle to demonstrate with ng-pattern as requested:
http://jsfiddle.net/HB7LU/15632/
<input ng-model="myText" ng-pattern="/^(0*[1-9]+0*)+$/" type="text" />
It is better to put regex in your controller $scope variable, and bind it inside ng-patter.SEE THIS
$scope.regex = /([0]+[1-9]+[0]+)?$/;
ng-pattern="regex";
OR,
ng-pattern="^([0]+[1-9]+[0]+)?$"
You could use a regEx pattern that checks if the input contains at least one number:
ng-pattern=".*[0-9].*"
RegEx is from Regular Expression For At Least One Number
Try this RegEx : ng-pattern="/^(?!0+$)\d{10}$/" using look-ahead.

ng-pattern Validation not working

I am trying to do validation with angular and Bootstrap but for some reasons ng-pattern validation is not working in Zip Code. Any help will be great in this.
Here's FIDDLE LINK
Not able to paste full code, but here is how I am attempting to use the ng-pattern directive:
<input type="text" class="form-control" id="zipCode" name="zipCode"
ng-model="auth.zip" ng-pattern="^(\d{5}-\d{4}|\d{5})$" required="">
The problem is that when you place the pattern inline instead of a scope variable it expects / around the regex, like a litteral.
Like this:
/^(\d{5}-\d{4}|\d{5})$/
Fiddle
See the input docs, check out the arguments section then ngPattern
Hi you can use like this
$scope.zipNumbr = /^[0-9]{1,6}$/;

Resources