Solved

validate username using custom directives in angularjs

Posted on 2014-02-03
2
815 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

695 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