File tree Expand file tree Collapse file tree 3 files changed +34
-5
lines changed
Expand file tree Collapse file tree 3 files changed +34
-5
lines changed Original file line number Diff line number Diff line change @@ -85,3 +85,21 @@ export const luhnChecksumValidate = (
8585
8686 return sum % digits . length === 0 ;
8787} ;
88+
89+ export const addPrefixForMobile = ( number : string ) : string => {
90+ const pregix = "+30" ;
91+ const cleanedNumber = number . replace ( / [ \s \- ( ) \. ] / g, "" ) ;
92+
93+ // Check if the phone number starts with the Greek international prefix or '0030'
94+ if ( cleanedNumber . startsWith ( pregix ) || cleanedNumber . startsWith ( "0030" ) ) {
95+ return cleanedNumber ;
96+ }
97+
98+ // If the phone number starts with '69' (mobile), add the international prefix
99+ if ( cleanedNumber . startsWith ( "69" ) ) {
100+ return pregix + cleanedNumber ;
101+ }
102+
103+ // If the phone number doesn't match the expected formats, return the original phone number
104+ return number ;
105+ } ;
Original file line number Diff line number Diff line change 1+ type PhoneNumberType = "mobile" | "landline" | "" ;
2+
13/**
24 * Validates Greek mobile and landline phone numbers.
35 *
46 * @param {string } value - The phone number to validate.
7+ * @param {PhoneNumberType } [type] - Optional. The type of phone number to validate ('mobile', 'landline', or ''). Defaults to '' (both).
58 * @returns {boolean } - Returns true if the phone number is valid, otherwise false.
69 */
7- export default ( value : string ) : boolean => {
10+ export default ( value : string , type : PhoneNumberType = "" ) : boolean => {
811 const cleanedValue = value . replace ( / [ \s \- ( ) \. ] / g, "" ) ;
912
1013 // Validate mobile numbers
1114 const mobileRegex = / ^ ( \+ 3 0 | 0 0 3 0 ) ? 6 9 \d { 8 } $ / ;
12- if ( mobileRegex . test ( cleanedValue ) ) {
13- return true ;
15+ if ( type === "mobile" || type === "" ) {
16+ if ( mobileRegex . test ( cleanedValue ) ) {
17+ return true ;
18+ }
1419 }
1520
1621 // Validate landline numbers with area codes
1722 const landlineRegex = / ^ ( \+ 3 0 | 0 0 3 0 ) ? ( 2 \d { 9 } | 8 0 1 \d { 7 } ) $ / ;
18- if ( landlineRegex . test ( cleanedValue ) ) {
19- return true ;
23+ if ( type === "landline" || type === "" ) {
24+ if ( landlineRegex . test ( cleanedValue ) ) {
25+ return true ;
26+ }
2027 }
2128
2229 return false ;
Original file line number Diff line number Diff line change @@ -20,4 +20,8 @@ describe.concurrent("isPhone", () => {
2020 it ( "00302101234567" , ( ) => {
2121 expect ( isPhone ( "00302101234567" ) ) . toBe ( true ) ;
2222 } ) ;
23+
24+ it ( "00302101234567" , ( ) => {
25+ expect ( isPhone ( "00302101234567" , "mobile" ) ) . toBe ( false ) ;
26+ } ) ;
2327} ) ;
You can’t perform that action at this time.
0 commit comments