<html ng-app="backend">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>
<script src="validator.js"></script>
<style>
.error {
color: red;
}
.error input{
border: solid 1px red;
}
input:focus {
border: solid 1px red;
}
</style>
</head>
<body>
<form id="frmSurvey" name="frmSurvey" ng-controller="SurveyController">
<div>
<lable for="article_url">URL:</lable>
<input type="text" name="article_url" required
check-valid-url
ng-class="{error: (frmSurvey.article_url.$invalid && frmSurvey.article_url.$dirty)}"
ng-forcus="(frmSurvey.article_url.$invalid && frmSurvey.article_url.$dirty)"
ng-model="surveyForm.articleUrl"/>
<span class="error" ng-if="isFormChecked && frmSurvey.article_url.$error.required">required</span>
<span class="error" ng-if="frmSurvey.article_url.$error.pattern">invalid url</span>
</div>
<div>
<button type="button" ng-disabled="frmSurvey.$invalid" ng-click="submitForm()">Submit</button>
<button type="button" ng-click="submitForm()">Submit</button>
</div>
<div ng-if="isFrmValid"><h1>Valid</h1></div>
</form>
</body>
</html>
//diretive
let backend = angular.module('backend', []);
backend.directive('checkValidUrl',checkValidUrl);
function checkValidUrl($timeout,$http){
return {
restrict: 'A',
// require NgModelController, i.e. require a controller of ngModel directive
require: 'ngModel',
// create linking function and pass in our NgModelController as a 4th argument
link: function(scope, element, attr, ctrl) {
let urlregex = /^[a-zA-Z0-9_-]*$/;
function doValid(ngModelValue) {
if (urlregex.test(ngModelValue)) {
ctrl.$setValidity('pattern', true);
} else {
ctrl.$setValidity('pattern', false);
}
if (ngModelValue.length <= 30) {
ctrl.$setValidity('maxLength', true);
} else {
ctrl.$setValidity('maxLength', false);
}
// we need to return our ngModelValue, to be displayed to the user(value of the input)
return ngModelValue;
}
// we need to add our doValid function to an array of other(build-in or custom) functions
// I have not notice any performance issues, but it would be worth investigating how much
// effect does this have on the performance of the app
ctrl.$parsers.push(doValid);
}
};
}
//controller code
let SurveyController = function ($scope) {
$scope.submitForm = function () {
$scope.isFormChecked = 1;
$scope.isFrmValid = 0;
console.log($scope);
if($scope.frmSurvey.$valid){
$scope.isFrmValid = 1;
}
else{
if($scope.frmSurvey.$error){
if($scope.frmSurvey.$error.required){
$scope.frmSurvey.$error.required[0].$$element[0].focus();
}else if($scope.frmSurvey.$error.pattern){
$scope.frmSurvey.$error.pattern[0].$$element[0].focus();
}
}
}
};
}
SurveyController.$inject = ['$scope']; //inject
backend.controller('SurveyController', SurveyController); //register SurveyController with backend module
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |