Skip to content

Commit

Permalink
improve Login page error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
parasharrajat committed Oct 19, 2021
1 parent b986ef8 commit 500ba78
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/libs/ValidationUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ function isValidUSPhone(phoneNumber) {
return CONST.REGEX.PHONE_E164_PLUS.test(phoneNumber.replace(CONST.REGEX.NON_ALPHA_NUMERIC, '')) && CONST.REGEX.US_PHONE.test(phoneNumber);
}

/**
* Checks whether a value is a numeric string including `(`, `)`, `-` and optional leading `+`
* @param {String} input
* @returns {Boolean}
*/
function isNumericWithSpecialChars(input) {
return /^\+?\d*$/.test(input.replace(/[()-]/g, ''));
}

export {
meetsAgeRequirements,
isValidAddress,
Expand All @@ -226,4 +235,5 @@ export {
isValidUSPhone,
isValidURL,
validateIdentity,
isNumericWithSpecialChars,
};
3 changes: 2 additions & 1 deletion src/libs/actions/Session.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Timers from '../Timers';
import * as Pusher from '../Pusher/pusher';
import NetworkConnection from '../NetworkConnection';
import {getUserDetails} from './User';
import {isNumericWithSpecialChars} from '../ValidationUtils';

let credentials = {};
Onyx.connect({
Expand Down Expand Up @@ -142,7 +143,7 @@ function fetchAccountDetails(login) {
}
} else if (response.jsonCode === 402) {
Onyx.merge(ONYXKEYS.ACCOUNT, {
error: CONST.REGEX.DIGITS_AND_PLUS.test(login)
error: isNumericWithSpecialChars(login)
? translateLocal('messages.errorMessageInvalidPhone')
: translateLocal('loginForm.error.invalidFormatEmailLogin'),
});
Expand Down
4 changes: 2 additions & 2 deletions src/pages/signin/LoginForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import canFocusInputOnScreenFocus from '../../libs/canFocusInputOnScreenFocus';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import getEmailKeyboardType from '../../libs/getEmailKeyboardType';
import ExpensiTextInput from '../../components/ExpensiTextInput';
import CONST from '../../CONST';
import {isNumericWithSpecialChars} from '../../libs/ValidationUtils';

const propTypes = {
/* Onyx Props */
Expand Down Expand Up @@ -80,7 +80,7 @@ class LoginForm extends React.Component {
}

if (!Str.isValidEmail(this.state.login) && !Str.isValidPhone(this.state.login)) {
if (CONST.REGEX.DIGITS_AND_PLUS.test(this.state.login)) {
if (isNumericWithSpecialChars(this.state.login)) {
this.setState({formError: 'messages.errorMessageInvalidPhone'});
} else {
this.setState({formError: 'loginForm.error.invalidFormatEmailLogin'});
Expand Down

0 comments on commit 500ba78

Please sign in to comment.