Skip to content

Instantly share code, notes, and snippets.

View guillefd's full-sized avatar
🤖
Building

Guillermo guillefd

🤖
Building
View GitHub Profile
@guillefd
guillefd / form-validation-pattern.component.ts
Created January 27, 2018 16:21
Angular Form Validator Pattern: only letters and numbers
import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
@Component({
selector: 'form',
templateUrl: './form.component.html',
styleUrls: ['./form.component.scss']
})
export class Form {
@guillefd
guillefd / form.component.html
Last active December 8, 2022 12:32
Angular Form Validator: Password Strength with upper, lower and number
<form [formGroup]="form">
<input type="password"
formControlName="password">
<p class="help is-danger"
*ngIf="form.get('password').hasError('strong')">
Must have at least one number, one lowercase and one uppercase letter.</p>
</form>
@guillefd
guillefd / form.component.ts
Created January 31, 2018 22:27
Angular, form checkbox, get check status when clicked
import { Component } from '@angular/core';
@Component({
selector: 'form',
templateUrl: `
<form>
<input type="checkbox" (click)="selected($event)"
</form>`,
styleUrls: ['./form.scss']
})
@guillefd
guillefd / array.js
Created February 1, 2018 02:11
Javascript Array: find item index and remove from Array
function findAndRemove(array, item) {
const index = array.indexOf(item);
if(index>-1) {
array.splice(index, 1);
}
return array;
}
@guillefd
guillefd / custom-validations.js
Created February 3, 2018 18:46
JS Regex Validations
function isInputValid(value, type) {
let valid;
let errorTxt;
switch(type) {
case 'email':
valid = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(value);
errorTxt = 'Enter a valida email';
break;
case 'phone':
valid = /^([-0-9.()+ ]{5,50})*$/.test(value);
@guillefd
guillefd / form.component.ts
Last active February 6, 2018 20:29
Angular, form value change observer
import { Component } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
@Component({
selector: 'form',
templateUrl: `
<form [formGroup]="form">
<input type="text" formControlName="name">
</form>
`,
@guillefd
guillefd / sanitize.service.ts
Created February 7, 2018 13:52
Angular, sanitize string: remove tags and urls
import { Injectable } from '@angular/core';
@Injectable()
export class SecurityService {
constructor() { }
sanitizeAllTags(text:string) {
return text.replace(/<[^>]*>/g, '*');
}
@guillefd
guillefd / element.component.ts
Created February 7, 2018 22:09
Angular, get HTML Element by ID
import { Component, OnInit, EventEmitter, Output, ElementRef, ViewChild } from '@angular/core';
@Component({
selector: 'form-place',
template: `<div #gmap>You got me!</div>`,
styleUrls: ['./form-place.component.scss']
})
export class elementComponent implements OnInit {
@ViewChild("gmap", {read: ElementRef}) gmap: ElementRef;
@guillefd
guillefd / autocomplete.component.ts
Last active July 3, 2020 15:15
Angular, get places/address autocomplete predictions with Google Maps Place API
import { Component } from '@angular/core';
import { FormGroup, FormBuilder } from '@angular/forms';
import { } from '@types/googlemaps';
@Component({
selector: 'autocomplete',
template: `
<input class="input"
type="text"
[(ngModel)]="autocompleteInput"
@guillefd
guillefd / javascript-promise-timeout.js
Created February 18, 2018 14:59 — forked from john-doherty/javascript-promise-timeout.js
Adds a timeout to a JavaScript promise, rejects if not resolved within timeout period
/**
* wraps a promise in a timeout, allowing the promise to reject if not resolve with a specific period of time
* @param {integer} ms - milliseconds to wait before rejecting promise if not resolved
* @param {Promise} promise to monitor
* @Example
* promiseTimeout(1000, fetch('https://courseof.life/johndoherty.json'))
* .then(function(cvData){
* alert(cvData);
* })
* .catch(function(){