Skip to content
Closed
1 change: 1 addition & 0 deletions src/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ const CONST = {
REGEX: {
SPECIAL_CHARS_WITHOUT_NEWLINE: /((?!\n)[()-\s\t])/g,
US_PHONE: /^\+1\d{10}$/,
US_PHONE_WITH_OPTIONAL_COUNTRY_CODE: /^(\+1)?\d{10}$/,
DIGITS_AND_PLUS: /^\+?[0-9]*$/,
PHONE_E164_PLUS: /^\+?[1-9]\d{1,14}$/,
PHONE_WITH_SPECIAL_CHARS: /^[+]*[(]{0,1}[0-9]{1,3}[)]{0,1}[-\s\\./0-9]{0,12}$/,
Expand Down
4 changes: 2 additions & 2 deletions src/components/OptionsSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import CONST from '../CONST';
import styles from '../styles/styles';
import optionPropTypes from './optionPropTypes';
import withLocalize, {withLocalizePropTypes} from './withLocalize';
import TextInput from './TextInput';
import PhoneTextInput from './PhoneTextInput';

const propTypes = {
/** Wether we should wait before focusing the TextInput, useful when using transitions */
Expand Down Expand Up @@ -217,7 +217,7 @@ class OptionsSelector extends Component {
return (
<View style={[styles.flex1]}>
<View style={[styles.ph5, styles.pv3]}>
<TextInput
<PhoneTextInput
Comment thread
thesahindia marked this conversation as resolved.
Resolved
ref={el => this.textInput = el}
value={this.props.value}
onChangeText={(text) => {
Expand Down
40 changes: 40 additions & 0 deletions src/components/PhoneTextInput/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
Comment thread
thesahindia marked this conversation as resolved.
Resolved
import * as baseTextInputPropTypes from '../TextInput/baseTextInputPropTypes';
import TextInput from '../TextInput';
import * as LoginUtils from '../../libs/LoginUtils';

class PhoneTextInput extends React.Component {
constructor(props) {
super(props);

this.state = {
value: props.value || props.defaultValue || '',
};
this.setValue = this.setValue.bind(this);
}

setValue(value) {
this.setState({value});
this.props.onChangeText(LoginUtils.getPhoneNumberWithoutSpecialChars(value));
}

render() {
return (
<TextInput
ref={this.props.forwardedRef}
// eslint-disable-next-line react/jsx-props-no-spreading
{...this.props}
value={this.state.value}
onChangeText={this.setValue}
/>
);
}
}

PhoneTextInput.propTypes = baseTextInputPropTypes.propTypes;
PhoneTextInput.defaultProps = baseTextInputPropTypes.defaultProps;

export default React.forwardRef((props, ref) => (
/* eslint-disable-next-line react/jsx-props-no-spreading */
<PhoneTextInput {...props} forwardedRef={ref} />
));
4 changes: 2 additions & 2 deletions src/languages/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export default {
lastName: 'Last name',
phone: 'Phone',
phoneNumber: 'Phone number',
phonePlaceholder: '(xxx)xxx-xxxx',
errorMessageInvalidPhone: 'If you\'re outside the US please include your country code, eg. +447782339811',
Comment thread
thesahindia marked this conversation as resolved.
Resolved
email: 'Email',
and: 'and',
details: 'Details',
Expand Down Expand Up @@ -568,7 +570,6 @@ export default {
callMeByMyName: 'Call me by my name',
},
messages: {
errorMessageInvalidPhone: 'Please enter a valid phone number without brackets or dashes. If you\'re outside the US please include your country code, eg. +447782339811',
maxParticipantsReached: 'You\'ve reached the maximum number of participants for a group chat.',
},
onfidoStep: {
Expand Down Expand Up @@ -690,7 +691,6 @@ export default {
confirmCompanyIsNot: 'I confirm that this company is not on the',
listOfRestrictedBusinesses: 'list of restricted businesses',
incorporationDatePlaceholder: 'Start date (yyyy-mm-dd)',
companyPhonePlaceholder: 'Phone Number (xxx)xxx-xxxx',
incorporationTypes: {
LLC: 'LLC',
CORPORATION: 'Corp',
Expand Down
4 changes: 2 additions & 2 deletions src/languages/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export default {
lastName: 'Apellido',
phone: 'teléfono',
phoneNumber: 'Número de teléfono',
phonePlaceholder: '(prefijo) + (número)',
errorMessageInvalidPhone: 'Si reside fuera de Estados Unidos, por favor incluye el prefijo internacional. P. ej. +447782339811',
Comment thread
thesahindia marked this conversation as resolved.
Resolved
email: 'Email',
and: 'y',
details: 'Detalles',
Expand Down Expand Up @@ -568,7 +570,6 @@ export default {
callMeByMyName: 'Llámame por mi nombre',
},
messages: {
errorMessageInvalidPhone: 'Por favor, introduce un número de teléfono válido sin paréntesis o guiones. Si reside fuera de Estados Unidos, por favor incluye el prefijo internacional. P. ej. +447782339811',
maxParticipantsReached: 'Has llegado al número máximo de participantes para un grupo.',
},
onfidoStep: {
Expand Down Expand Up @@ -692,7 +693,6 @@ export default {
confirmCompanyIsNot: 'Confirmo que esta empresa no está en el',
listOfRestrictedBusinesses: 'lista de negocios restringidos',
incorporationDatePlaceholder: 'Fecha de inicio (aaaa-mm-dd)',
companyPhonePlaceholder: '(prefijo) + (número)',
incorporationTypes: {
LLC: 'LLC',
CORPORATION: 'Corp',
Expand Down
10 changes: 10 additions & 0 deletions src/libs/LoginUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ function getPhoneNumberWithoutSpecialChars(phone) {
return phone.replace(CONST.REGEX.SPECIAL_CHARS_WITHOUT_NEWLINE, '');
}

/**
* Remove +1 from the phone number
* @param {String} phone
* @return {String}
*/
function getPhoneNumberWithoutUSCountryCode(phone) {
return phone.replace(/^\+1/, '');
}

export {
getEmailWithoutMergedAccountPrefix,
getPhoneNumberWithoutSpecialChars,
getPhoneNumberWithoutUSCountryCode,
};
4 changes: 2 additions & 2 deletions src/libs/OptionsListUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -781,14 +781,14 @@ function getHeaderMessage(hasSelectableOptions, hasUserToInvite, searchValue, ma
}

if (searchValue && CONST.REGEX.DIGITS_AND_PLUS.test(searchValue) && !Str.isValidPhone(searchValue)) {
return Localize.translate(preferredLocale, 'messages.errorMessageInvalidPhone');
return Localize.translate(preferredLocale, 'common.errorMessageInvalidPhone');
}

// Without a search value, it would be very confusing to see a search validation message.
// Therefore, this skips the validation when there is no search value.
if (searchValue && !hasSelectableOptions && !hasUserToInvite) {
if (/^\d+$/.test(searchValue)) {
return Localize.translate(preferredLocale, 'messages.errorMessageInvalidPhone');
return Localize.translate(preferredLocale, 'common.errorMessageInvalidPhone');
}

return Localize.translate(preferredLocale, 'common.noResultsFound');
Expand Down
9 changes: 7 additions & 2 deletions src/libs/ValidationUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,16 @@ function validateIdentity(identity) {

/**
* @param {String} phoneNumber
* @param {Boolean} [isCountryCodeOptional]
* @returns {Boolean}
*/
function isValidUSPhone(phoneNumber) {
function isValidUSPhone(phoneNumber, isCountryCodeOptional) {
const isUsPhone = isCountryCodeOptional
? CONST.REGEX.US_PHONE_WITH_OPTIONAL_COUNTRY_CODE.test(phoneNumber) : CONST.REGEX.US_PHONE.test(phoneNumber);

// Remove alphanumeric characters and validate that this is in fact a phone number
return CONST.REGEX.PHONE_E164_PLUS.test(phoneNumber.replace(CONST.REGEX.NON_ALPHA_NUMERIC, '')) && CONST.REGEX.US_PHONE.test(phoneNumber);
return CONST.REGEX.PHONE_E164_PLUS.test(phoneNumber.replace(CONST.REGEX.NON_ALPHA_NUMERIC, ''))
&& isUsPhone;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/Session/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function fetchAccountDetails(login) {
} else if (response.jsonCode === 402) {
Onyx.merge(ONYXKEYS.ACCOUNT, {
error: ValidationUtils.isNumericWithSpecialChars(login)
? Localize.translateLocal('messages.errorMessageInvalidPhone')
? Localize.translateLocal('common.errorMessageInvalidPhone')
: Localize.translateLocal('loginForm.error.invalidFormatEmailLogin'),
});
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/libs/actions/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ function setSecondaryLoginAndNavigate(login, password) {
if (error.includes('already belongs to an existing Expensify account.')) {
error = 'This login already belongs to an existing Expensify account.';
}
if (error.includes('I couldn\'t validate the phone number')) {
error = Localize.translateLocal('common.errorMessageInvalidPhone');
}

Onyx.merge(ONYXKEYS.USER, {error});
}).finally(() => {
Expand Down
14 changes: 12 additions & 2 deletions src/pages/EnablePayments/AdditionalDetailsStep.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import * as ValidationUtils from '../../libs/ValidationUtils';
import AddressSearch from '../../components/AddressSearch';
import DatePicker from '../../components/DatePicker';
import FormHelper from '../../libs/FormHelper';
import PhoneTextInput from '../../components/PhoneTextInput';
import * as LoginUtils from '../../libs/LoginUtils';
import walletAdditionalDetailsDraftPropTypes from './walletAdditionalDetailsDraftPropTypes';
import personalDetailsPropType from '../personalDetailsPropType';
import * as PersonalDetails from '../../libs/actions/PersonalDetails';
Expand Down Expand Up @@ -171,6 +173,10 @@ class AdditionalDetailsStep extends React.Component {
errors.ssn = true;
}

if (!ValidationUtils.isValidUSPhone(this.props.walletAdditionalDetailsDraft.phoneNumber, true)) {
errors.phoneNumber = true;
}

_.each(this.requiredFields, (requiredField) => {
if (ValidationUtils.isRequiredFulfilled(this.props.walletAdditionalDetailsDraft[requiredField])) {
return;
Expand All @@ -189,7 +195,10 @@ class AdditionalDetailsStep extends React.Component {
}

BankAccounts.activateWallet(CONST.WALLET.STEP.ADDITIONAL_DETAILS, {
personalDetails: this.props.walletAdditionalDetailsDraft,
personalDetails: {
...this.props.walletAdditionalDetailsDraft,
phoneNumber: LoginUtils.getPhoneNumberWithoutUSCountryCode(this.props.walletAdditionalDetailsDraft.phoneNumber),
},
});
}

Expand Down Expand Up @@ -310,9 +319,10 @@ class AdditionalDetailsStep extends React.Component {
</>
) : null}
</View>
<TextInput
<PhoneTextInput
Comment thread
thesahindia marked this conversation as resolved.
Resolved
containerStyles={[styles.mt4]}
label={this.props.translate(this.fieldNameTranslationKeys.phoneNumber)}
placeholder={this.props.translate('common.phonePlaceholder')}
onChangeText={val => this.clearErrorAndSetValue('phoneNumber', val)}
value={this.props.walletAdditionalDetailsDraft.phoneNumber || ''}
errorText={this.getErrorText('phoneNumber')}
Expand Down
14 changes: 10 additions & 4 deletions src/pages/ReimbursementAccount/CompanyStep.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import * as ReimbursementAccountUtils from '../../libs/ReimbursementAccountUtils
import reimbursementAccountPropTypes from './reimbursementAccountPropTypes';
import ReimbursementAccountForm from './ReimbursementAccountForm';
import AddressForm from './AddressForm';
import PhoneTextInput from '../../components/PhoneTextInput';
import * as LoginUtils from '../../libs/LoginUtils';

const propTypes = {
/** Bank account currently in setup */
Expand Down Expand Up @@ -154,7 +156,7 @@ class CompanyStep extends React.Component {
errors.incorporationDateFuture = true;
}

if (!ValidationUtils.isValidPhoneWithSpecialChars(this.state.companyPhone)) {
if (!ValidationUtils.isValidUSPhone(this.state.companyPhone, true)) {
errors.companyPhone = true;
}

Expand All @@ -176,7 +178,11 @@ class CompanyStep extends React.Component {
}

const incorporationDate = moment(this.state.incorporationDate).format(CONST.DATE.MOMENT_FORMAT_STRING);
BankAccounts.setupWithdrawalAccount({...this.state, incorporationDate});
BankAccounts.setupWithdrawalAccount({
...this.state,
companyPhone: LoginUtils.getPhoneNumberWithoutUSCountryCode(this.state.companyPhone),
incorporationDate,
});
}

render() {
Expand Down Expand Up @@ -236,13 +242,13 @@ class CompanyStep extends React.Component {
this.clearErrors(_.keys(renamedValues));
}}
/>
<TextInput
<PhoneTextInput
Comment thread
thesahindia marked this conversation as resolved.
Resolved
label={this.props.translate('common.phoneNumber')}
containerStyles={[styles.mt4]}
keyboardType={CONST.KEYBOARD_TYPE.PHONE_PAD}
onChangeText={value => this.clearErrorAndSetValue('companyPhone', value)}
value={this.state.companyPhone}
placeholder={this.props.translate('companyStep.companyPhonePlaceholder')}
placeholder={this.props.translate('common.phonePlaceholder')}
errorText={this.getErrorText('companyPhone')}
maxLength={CONST.PHONE_MAX_LENGTH}
/>
Expand Down
15 changes: 7 additions & 8 deletions src/pages/RequestCallPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import Text from '../components/Text';
import Section from '../components/Section';
import KeyboardAvoidingView from '../components/KeyboardAvoidingView';
import * as Illustrations from '../components/Icon/Illustrations';
import * as Expensicons from '../components/Icon/Expensicons';
import * as LoginUtils from '../libs/LoginUtils';
import * as ValidationUtils from '../libs/ValidationUtils';
import * as PersonalDetails from '../libs/actions/PersonalDetails';
import PhoneTextInput from '../components/PhoneTextInput';
import * as Expensicons from '../components/Icon/Expensicons';
import * as User from '../libs/actions/User';

const propTypes = {
Expand Down Expand Up @@ -148,7 +148,7 @@ class RequestCallPage extends Component {
policyID: policyForCall.id,
firstName: this.state.firstName,
lastName: this.state.lastName,
phoneNumber: LoginUtils.getPhoneNumberWithoutSpecialChars(this.state.phoneNumber),
phoneNumber: this.state.phoneNumber,
phoneNumberExtension: this.state.phoneExtension,
});
}
Expand All @@ -170,9 +170,8 @@ class RequestCallPage extends Component {
* @returns {String}
*/
getPhoneNumberError() {
const phoneNumber = LoginUtils.getPhoneNumberWithoutSpecialChars(this.state.phoneNumber);
if (_.isEmpty(this.state.phoneNumber.trim()) || !Str.isValidPhone(phoneNumber)) {
return this.props.translate('messages.errorMessageInvalidPhone');
if (_.isEmpty(this.state.phoneNumber) || !Str.isValidPhone(this.state.phoneNumber)) {
return this.props.translate('common.errorMessageInvalidPhone');
}
return '';
}
Expand Down Expand Up @@ -278,12 +277,12 @@ class RequestCallPage extends Component {
onChangeLastName={lastName => this.setState({lastName})}
style={[styles.mv4]}
/>
<TextInput
<PhoneTextInput
label={this.props.translate('common.phoneNumber')}
name="phone"
autoCorrect={false}
value={this.state.phoneNumber}
placeholder="2109400803"
placeholder={this.props.translate('common.phonePlaceholder')}
Comment thread
thesahindia marked this conversation as resolved.
Resolved
errorText={this.state.phoneNumberError}
onBlur={this.validatePhoneInput}
onChangeText={phoneNumber => this.setState({phoneNumber})}
Expand Down
Loading