Solved

validate username using custom directives in angularjs

Posted on 2014-02-03
2
758 Views
Last Modified: 2014-02-05
Hi Experts ,

I have to validate username  (no special characters & digits)  across different forms / pages in AngularJS. So I want to create custom directive that can  be used at multiple forms/pages. I need sample working code using AngularJS.

Thanks in Advance
0
Comment
Question by:ksd123
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39831376
What about : http://jsfiddle.net/97ZRb/
you just need to add good-username in your username field

	App.directive("goodUsername", function() {
		return {
			require: "ngModel",
			restrict: "A",
			link: function(scope, el, attrs, ctrl) {
				ctrl.$parsers.unshift(function(viewValue) {
					var result = !/[^a-z0-9]/.test(viewValue);
					ctrl.$setValidity("goodUsername", result);
                    return viewValue;
				});
			}
		}
	});

Open in new window

0
 
LVL 9

Expert Comment

by:Sar1973
ID: 39832048
Which criteria do you want to be applied to your username (es. only characters)?
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now