10 examples of 'simple phone number validation in javascript' in JavaScript

Every line of 'simple phone number validation in javascript' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
40function isValidPhone(value) {
41 return /^[0-9]+$/ig.test(value)
42}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

28export function phoneValidator(rule, value, callback) {
29 const INVALID_PHONE_MSG = 'Número de telefone inválido';
30 const PHONE_REGEX = /^\([0-9]*\)\s*[0-9]*\s*-*\s*[0-9]*/i;
31
32 if (!rule.required && !value) {
33 callback();
34 }
35
36 if (PHONE_REGEX.test(value)) {
37 callback();
38 } else {
39 callback(INVALID_PHONE_MSG);
40 }
41}
269static phone(value) {
270 var reg = /^((\d{10,11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)$/;
271 return reg.test(value);
272}
21export function isValidLength(phone: string): boolean {
22 return phone.length >= PHONE_MIN_LENGTH && phone.length <= PHONE_MAX_LENGTH;
23}
16function js_phone( phone )
17{
18 if ( parseInt( phone ) == 0 )
19 return '';
20 var len = phone.length;
21 var result = phone;
22 if ( len > 10 )
23 result = result.insert( -10, ' (' ).insert( -7, ') ');
24 if ( len > 4 )
25 result = result.insert( -4, '-' ).insert( -2 , '-' );
26 return ( len > 10 ? '+' : '' ) + result;
27}
42public getDigits(phone: string): string {
43 const numbers = phone.match(/\d+/g)
44 if (numbers === null) return ''
45 return numbers.join('')
46}
312validPhone() {
313 try {
314 const v = this.phone;
315 return (v && _isValidType('string', v));
316 } catch (err) {
317 return false;
318 }
319}
123function InvalidPhone(){
124 var invalid_phone_number = $("#invalid-phone-number");
125 invalid_phone_number.attr("attrHidden", "false");
126 var need_phone_number = $("#need-phone-number");
127 need_phone_number.attr("attrHidden", "true");
128 window.phone_ok = false;
129}
128checkWorkPhone: function checkWorkPhone(entry, value) {
129 return !value;
130},
35function validateZIPCode(value){
36 if (!isZIPCode(value)) {
37 alert("Please enter a ZIP code in the form xxxxx.");
38 };
39}

Related snippets