Skip to content

Commit

Permalink
Merge pull request #35 from takayukister/dev/1.4
Browse files Browse the repository at this point in the history
Improve isTelephoneNumber validation
  • Loading branch information
takayukister authored Sep 24, 2024
2 parents 3b49453 + d4b1969 commit 54516e2
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion rules/tel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,24 @@ export const tel = function ( formDataTree ) {
.map( val => val.trim() ).filter( val => '' !== val );

const isTelephoneNumber = text => {
text = text.replace( /[#*].*$/, '' ); // Remove extension
text = text.replaceAll( /[()/.*#\s-]+/g, '' );

return /^[+]?[0-9]+$/.test( text );
const international = text.startsWith( '+' ) || text.startsWith( '00' );

if ( international ) {
text = `+${ text.replace( /^[+0]+/, '' ) }`;
}

if ( ! /^[+]?[0-9]+$/.test( text ) ) {
return false;
}

if ( ! ( 6 < text.length && text.length < 16 ) ) {
return false;
}

return true;
};

if ( ! values.every( isTelephoneNumber ) ) {
Expand Down

0 comments on commit 54516e2

Please sign in to comment.