Skip to content

Commit 77a3687

Browse files
committed
Add validCharactersOnly prop to prevent invalid characters (letters,...)
1 parent 21ac7dc commit 77a3687

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,14 @@ Read more on `vue-form-generator`'s [instruction page](https://icebob.gitbooks.i
107107
| `preferredCountries` | `Array` | `[]` | Preferred countries list, will be on top of the dropdown. ie `['AU', 'BR']` |
108108
| `onlyCountries` | `Array` | `[]` | List of countries will be shown on the dropdown. ie `['AU', 'BR']` |
109109
| `ignoredCountries` | `Array` | `[]` | List of countries will NOT be shown on the dropdown. ie `['AU', 'BR']` |
110-
| `autocomplete`| `String` | `'on'` | Native input 'autocomplete' attribute |
111-
| `name`| `String` | `'telephone'` | Native input 'name' attribute |
112-
| `maxLen`| `Number` | `25` | Native input 'maxlength' attribute |
113-
| `wrapperClasses`| `String | Array | Object` | `''` | Custom classes for the wrapper |
114-
| `inputClasses`| `String | Array | Object` | `''` | Custom classes for the `input` |
115-
| `dropdownOptions`| `Object` | `{ disabledDialCode: false, tabindex: 0 }` | Options for dropdown, supporting `disabledDialCode` and `tabindex`|
116-
| `inputOptions`| `Object` | `{ showDialCode: false, tabindex: 0 }` | Options for input, supporting `showDialCode` (always show dial code in the input) and `tabindex`|
110+
| `autocomplete` | `String` | `'on'` | Native input 'autocomplete' attribute |
111+
| `name` | `String` | `'telephone'` | Native input 'name' attribute |
112+
| `maxLen` | `Number` | `25` | Native input 'maxlength' attribute |
113+
| `wrapperClasses` | `String | Array | Object` | `''` | Custom classes for the wrapper |
114+
| `inputClasses` | `String | Array | Object` | `''` | Custom classes for the `input` |
115+
| `dropdownOptions` | `Object` | `{ disabledDialCode: false, tabindex: 0 }` | Options for dropdown, supporting `disabledDialCode` and `tabindex`|
116+
| `inputOptions` | `Object` | `{ showDialCode: false, tabindex: 0 }` | Options for input, supporting `showDialCode` (always show dial code in the input) and `tabindex`|
117+
| `validCharactersOnly` | `Boolean` | `false` | Only allow valid characters in a phone number (will also verify in `mounted`, so phone number with invalid characters will be shown as an empty string) |
117118

118119
### Events
119120

src/vue-tel-input.vue

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ export default {
231231
type: Number,
232232
default: 25,
233233
},
234+
validCharactersOnly: {
235+
type: Boolean,
236+
default: false,
237+
},
234238
},
235239
mounted() {
236240
this.initializeCountry();
@@ -353,7 +357,12 @@ export default {
353357
} else {
354358
this.$emit('close');
355359
}
356-
}
360+
},
361+
phone(newValue, oldValue) {
362+
if (this.validCharactersOnly && !this.testCharacters()) {
363+
this.$nextTick(() => { this.phone = oldValue });
364+
}
365+
},
357366
},
358367
methods: {
359368
initializeCountry() {
@@ -419,7 +428,14 @@ export default {
419428
this.$emit('input', this.response.number, this.response);
420429
this.$emit('onInput', this.response); // Deprecated
421430
},
431+
testCharacters() {
432+
const re = /^[\(\)\-\+0-9\s]*$/;
433+
return re.test(this.phone);
434+
},
422435
onInput() {
436+
if (this.validCharactersOnly && !this.testCharacters()) {
437+
return;
438+
}
423439
this.$refs.input.setCustomValidity(this.response.isValid ? '' : this.invalidMsg);
424440
// Returns response.number to assign it to v-model (if being used)
425441
// Returns full response for cases @input is used and parent wants to return the whole response.

0 commit comments

Comments
 (0)