Skip to content

Commit 604fa5c

Browse files
authored
Merge pull request #152 from Alexandernoa/master
created the 'сustom-validate' prop to validate with regex
2 parents 2fda63a + e4aea24 commit 604fa5c

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

src/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<div style="width: 500px; margin: 20px auto;">
1010
<vue-tel-input
1111
:preferred-countries="['us', 'gb', 'ua']"
12-
:valid-characters-only="true"
12+
:custom-validate="/^[0-9]*$/"
1313
@input="onInput"/>
1414
</div>
1515
<div

src/components/vue-tel-input.vue

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ export default {
219219
type: Boolean,
220220
default: () => getDefault('validCharactersOnly'),
221221
},
222+
customValidate: {
223+
type: [Boolean, RegExp],
224+
default: () => getDefault('customValidate'),
225+
},
222226
dynamicPlaceholder: {
223227
type: Boolean,
224228
default: () => getDefault('dynamicPlaceholder'),
@@ -319,7 +323,9 @@ export default {
319323
}
320324
},
321325
phone(newValue, oldValue) {
322-
if (this.validCharactersOnly && !this.testCharacters()) {
326+
const isValidCharactersOnly = this.validCharactersOnly && !this.testCharacters();
327+
const isCustomValidate = this.customValidate && !this.testCustomValidate();
328+
if (isValidCharactersOnly || isCustomValidate) {
323329
this.$nextTick(() => { this.phone = oldValue; });
324330
} else if (newValue) {
325331
if (newValue[0] === '+') {
@@ -466,10 +472,16 @@ export default {
466472
const re = /^[()\-+0-9\s]*$/;
467473
return re.test(this.phone);
468474
},
475+
testCustomValidate() {
476+
return this.customValidate instanceof RegExp ? this.customValidate.test(this.phone) : false;
477+
},
469478
onInput(e) {
470479
if (this.validCharactersOnly && !this.testCharacters()) {
471480
return;
472481
}
482+
if (this.customValidate && !this.testCustomValidate()) {
483+
return;
484+
}
473485
this.$refs.input.setCustomValidity(this.phoneObject.valid ? '' : this.invalidMsg);
474486
// Returns response.number to assign it to v-model (if being used)
475487
// Returns full response for cases @input is used

src/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export const defaultOptions = {
5656
inputOptions: {},
5757
maxLen: 25,
5858
validCharactersOnly: false,
59+
customValidate: false,
5960
dynamicPlaceholder: false,
6061
};
6162

0 commit comments

Comments
 (0)