Avalados por :

Como validar um número de telefone sem aceitar a letra 'e' com regex - Guia passo a passo

  • Creado 01/03/2024
  • Modificado 01/03/2024
  • 7 Vistas
0
Cargando...

Tenho um campo de número de telefone em minha aplicação e quero validá-lo. Estou validando com regex, mas está aceitando a letra 'e' como entrada.

Você pode me ajudar com o regex que não aceita 'e'?

Isso é o que tentei.

View.xml

<Label text="Telefone *" />

<Input Text="Telefone" id="iPhone" liveChange="onValidate" maxLength="10"

placeholder="Insira seu número de celular de 10 dígitos" enabled="true" type="Number" />

view.controller.js

onValidate: function(oEvent){

var regex1 = "(iFname|iLname|onDetails|iCompany|iAddress|iCity|iProvice|iCountry|iPhone)";

var regex2 = "iZipcode";

var FirstName = ("^[\w'\-,.][^0-9_!¡?÷?¿/\\+=@#$%ˆ&*(){}|~<>;:[\]]{2,}$");

var Phoneregex = ("^(\+91[\-\s]?)?[0]?(91)?[789]\d{9}$)")

var Emailregex = new RegExp("^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z-]+\\.)+[a-zA-Z]{2,6}");

var FieldID = oEvent.getParameter("id");

var id = FieldID.split("--")[1];

this.temp = false;

switch (id) {

case "chk0":

if (this.getView().byId(id).getSelected() === true) {

this.getView().byId(id).setValueState("Success");

this.temp = true;

break;

}

if (this.getView().byId(id).getValue() !== "" && isNaN(this.getView().byId(id).getValue()) && id.match(regex1)) {

this.getView().byId(id).setValueState("Success");

this.temp = true;

}

else if (id.match("iFname") && this.getView().byId(id).getValue() !== "" && this.getView().byId(id).getValue().match(FirstName))

{

this.temp = true;

this.getView().byId(id).setValueState("Success");

}

else if (id.match("iLname") && this.getView().byId(id).getValue() !== "" && this.getView().byId(id).getValue().match(FirstName))

{

this.temp = true;

this.getView().byId(id).setValueState("Success");

}

else if (this.getView().byId(id).getValue() !== "" && !isNaN(this.getView().byId(id).getValue()) && id.match(regex2)) {

this.getView().byId(id).setValueState("Success");

this.temp = true;

}

else if (id.match("iPhone") && this.getView().byId(id).getValue() !== "" && this.getView().byId(id).getValue().match(Phoneregex))

{

this.temp = true;

this.getView().byId(id).setValueState("Success");

}

else if (id.match("iEmail") && this.getView().byId(id).getValue() !== "" && this.getView().byId(id).getValue().match(Emailregex))

{

this.temp = true;

this.getView().byId(id).setValueState("Success");

}

else {

this.getView().byId(id).setValueState("Error");

this.getView().byId(id).setValueStateText("Por favor, forneça informações válidas");

sap.m.MessageToast.show("Erro!");

this.temp = false;

}

return this.temp;

},

Pedro Pascal
Se unió el 07/03/2018
Pinterest
Telegram
Linkedin
Whatsapp

4 Respuestas

0
Cargando...

Olá, obrigado manju537449 , esta foi uma solução tão fácil para substituir 'e' por branco. Nunca pensei nisso.

Respondido el 15/04/2024
LUCIANO RIOJA GHIOTTO
Se unió el 13/07/2019
0
Cargando...

Olá Tanmay,

Tente usar o seguinte código.

<Input id="iPhone" liveChange="onValidate" maxLength="10"

placeholder="Insira seu número de telefone celular de 10 dígitos" enabled="true" />

JS:

onValidate: function(oEvent) {

var oSource = oEvent.getSource();

var val = oSource.getValue();

val = val.replace(/[^\d]/g, '');

oSource.setValue(val);

},

Saudações,

Manjunatha

Respondido el 15/04/2024
LUCIANO RIOJA GHIOTTO
Se unió el 13/07/2019
0
Cargando...

Tentei usar 'Tel' mas não funcionou. Não tenho problema em usar 'Número' ou 'Tel', o problema é que aceita 'e'.

Respondido el 15/04/2024
LUCIANO RIOJA GHIOTTO
Se unió el 13/07/2019
0
Cargando...

Existe alguma razão pela qual você não está usando o campo sap.m.Input com o tipo "Tel"? Isso permitiria obter isso diretamente (e até mesmo o teclado correto em dispositivos móveis).

Respondido el 15/04/2024
LUCIANO RIOJA GHIOTTO
Se unió el 13/07/2019

contacto@primeinstitute.com

(+51) 1641 9379
(+57) 1489 6964

© 2024 Copyright. Todos los derechos reservados.

Desarrollado por Prime Institute

¡Hola! Soy Diana, asesora académica de Prime Institute, indícame en que curso estas interesado, saludos!
Hola ¿Puedo ayudarte?