Per-Arne Andersen
4 years ago
2 changed files with 15 additions and 4 deletions
@ -1,12 +1,22 @@ |
|||
import { AbstractControl, ValidationErrors } from '@angular/forms'; |
|||
import * as IPCIDR from 'ip-cidr'; |
|||
import {Address4, Address6} from 'ip-address' |
|||
|
|||
export class IPValidator { |
|||
|
|||
static isIPAddress(control: AbstractControl): ValidationErrors | null { |
|||
if (!control.value || !(new IPCIDR(control.value).isValid())) { |
|||
return { validIP: true }; |
|||
} |
|||
return null; |
|||
|
|||
try { |
|||
new Address4(control.value) |
|||
return null |
|||
} catch (e) {} |
|||
try{ |
|||
new Address6(control.value) |
|||
return null |
|||
} catch (e) {} |
|||
return { validIP: true }; |
|||
} |
|||
|
|||
|
|||
|
|||
} |
|||
|
Loading…
Reference in new issue