Solved

validate username using custom directives in angularjs

Posted on 2014-02-03
2
797 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

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 …
This article discusses how to implement server side field validation and display customized error messages to the client.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

733 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