TT#46169 Phone number input validation

Change-Id: If04aa117b9aea1eb024f065d047638e3f113a48f
changes/45/24445/4
raxelsen 7 years ago
parent 399de962c5
commit 9d06ca52e5

@ -294,6 +294,7 @@
import { getChromeExtensionUrl } from '../helpers/cdk-lib' import { getChromeExtensionUrl } from '../helpers/cdk-lib'
import CscCallDialpad from './CscCallDialpad' import CscCallDialpad from './CscCallDialpad'
import CscPhoneNumberInput from './call/CscPhoneNumberInput' import CscPhoneNumberInput from './call/CscPhoneNumberInput'
import { showGlobalError } from '../helpers/ui'
import { import {
QLayout, QLayout,
QCard, QCard,
@ -384,10 +385,13 @@
call(localMedia) { call(localMedia) {
if(this.$refs.phoneNumberInput.hasPhoneNumber()) { if(this.$refs.phoneNumberInput.hasPhoneNumber()) {
this.$store.dispatch('call/start', { this.$store.dispatch('call/start', {
number: this.$refs.phoneNumberInput.getRawPhoneNumber(), number: this.$refs.phoneNumberInput.getPhoneNumber(),
localMedia: localMedia localMedia: localMedia
}); });
} }
else {
showGlobalError(this.$t('validationErrors.inputValidNumber'));
}
}, },
accept(localMedia) { accept(localMedia) {
this.$store.dispatch('call/accept', localMedia); this.$store.dispatch('call/accept', localMedia);

@ -4,7 +4,6 @@
dark dark
:count="maxLength" :count="maxLength"
:helper="helperMessage" :helper="helperMessage"
:error="$v.phoneNumber.$error"
:error-label="errorMessage" :error-label="errorMessage"
> >
<q-input <q-input
@ -13,22 +12,21 @@
clearable clearable
type="text" type="text"
:float-label="$t('call.number')" :float-label="$t('call.number')"
:value="phoneNumber"
:error="$v.phoneNumber.$error"
:max-length="maxLength"
:readonly="!enabled" :readonly="!enabled"
@input="inputPhoneNumber" v-model="phoneNumber"
@keypress.space.prevent @keypress.space.prevent
@keydown.space.prevent @keydown.space.prevent
@keyup.space.prevent @keyup.space.prevent
:after="inputButtons" :after="inputButtons"
@keyup.enter="keyReturn()" @keyup.enter="keyReturn()"
@input="$v.phoneNumber.$touch"
@blur="$v.phoneNumber.$touch"
:error="$v.phoneNumber.$error"
/> />
</q-field> </q-field>
<q-field <q-field
v-else v-else
dark dark
:error="$v.phoneNumber.$error"
:error-label="errorMessage" :error-label="errorMessage"
> >
<q-input <q-input
@ -37,30 +35,27 @@
clearable clearable
type="text" type="text"
:placeholder="$t('call.number')" :placeholder="$t('call.number')"
:value="phoneNumber"
:error="$v.phoneNumber.$error"
:max-length="maxLength"
:readonly="!enabled" :readonly="!enabled"
@input="inputPhoneNumber" v-model="phoneNumber"
@keypress.space.prevent @keypress.space.prevent
@keydown.space.prevent @keydown.space.prevent
@keyup.space.prevent @keyup.space.prevent
:after="inputButtons" :after="inputButtons"
@input="$v.phoneNumber.$touch"
@blur="$v.phoneNumber.$touch"
:error="$v.phoneNumber.$error"
/> />
</q-field> </q-field>
</template> </template>
<script> <script>
import Vue from 'vue' import Vue from 'vue'
import _ from 'lodash' import _ from 'lodash'
import { userInfo } from '../../helpers/validation'
import { import {
maxLength maxLength,
required
} from 'vuelidate/lib/validators' } from 'vuelidate/lib/validators'
import {
normalizeNumber,
rawNumber
} from '../../filters/number-format'
import { import {
QField, QField,
QInput QInput
@ -80,7 +75,9 @@
}, },
validations: { validations: {
phoneNumber: { phoneNumber: {
maxLength: maxLength(64) userInfo,
maxLength: maxLength(64),
required
} }
}, },
props: { props: {
@ -102,7 +99,20 @@
], ],
computed: { computed: {
errorMessage() { errorMessage() {
return this.$t('validationErrors.inputValidNumber'); if (!this.$v.phoneNumber.required) {
return this.$t('validationErrors.fieldRequired', {
field: this.$t('call.number')
});
}
else if (!this.$v.phoneNumber.maxLength) {
return this.$t('validationErrors.maxLength', {
field: this.$t('call.number'),
maxLength: this.$v.phoneNumber.$params.maxLength.max
});
}
else if (!this.$v.phoneNumber.userInfo) {
return this.$t('validationErrors.inputValidNumber');
}
}, },
helperMessage() { helperMessage() {
return this.$t('validationErrors.inputNumber'); return this.$t('validationErrors.inputNumber');
@ -130,21 +140,14 @@
keyReturn() { keyReturn() {
this.$emit('key-return'); this.$emit('key-return');
}, },
inputPhoneNumber(value) {
this.phoneNumber = normalizeNumber(value, this.isMobile);
this.$v.phoneNumber.$touch();
},
getPhoneNumber() { getPhoneNumber() {
return this.phoneNumber; return this.phoneNumber;
}, },
getRawPhoneNumber() {
return rawNumber(this.getPhoneNumber());
},
hasPhoneNumber() { hasPhoneNumber() {
return !_.isEmpty(this.phoneNumber); return !this.$v.phoneNumber.$error && !_.isEmpty(this.phoneNumber);
}, },
concat(str) { concat(str) {
this.inputPhoneNumber(this.phoneNumber + "" + str); this.phoneNumber = this.phoneNumber + "" + str;
}, },
focusInput() { focusInput() {
Vue.nextTick(() => { Vue.nextTick(() => {

@ -0,0 +1,5 @@
export function userInfo (value) {
var regex = new RegExp(/^[-_.!~*'&=+$,;?/%a-zA-Z0-9]+$/);
return regex.test(value);
}

@ -0,0 +1,30 @@
'use strict';
import { assert } from 'chai';
import { userInfo } from '../../src/helpers/validation'
describe('Userinfo validation helper', function() {
it('should validate userinfo consisting of phone number and country code', function() {
let input = "+439988776655";
assert.isTrue(userInfo(input));
});
it('should validate userinfo with parameter', function() {
let input = "+358-555-1234567;postd=pp22";
assert.isTrue(userInfo(input));
});
it('should validate userinfo consisting of subscriber username', function() {
let input = "alice";
assert.isTrue(userInfo(input));
});
it('should not validate invalid userinfo characters', function() {
let input = "al)<e";
assert.isFalse(userInfo(input));
});
});
Loading…
Cancel
Save