Avalados por :

Cómo validar un número de teléfono sin aceptar la letra 'e' con regex - Guía paso a paso

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

Tengo un campo de número de teléfono en mi aplicación y quiero validarlo. Lo estoy validando con regex pero está tomando la letra 'e' como entrada.

¿Puedes ayudarme con el regex que no acepte 'e'?

Esto es lo que he intentado.

View.xml

<Label text="Teléfono *" />

<Input Text="Teléfono" id="iPhone" liveChange="onValidate" maxLength="10"

placeholder="Ingresa tu número de móvil 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, proporciona información válida");

sap.m.MessageToast.show("¡Error!");

this.temp = false;

}

return this.temp;

},

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

4 Respuestas

0
Cargando...

Hola, Gracias manju537449 , esta fue una solución tan fácil para reemplazar 'e' con blanco. Nunca pensé en esto.

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

Hola Tanmay,

Intenta usar el siguiente código.

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

placeholder="Ingresa tu número de teléfono móvil 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);

},

Saludos,

Manjunatha

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

Intenté usar 'Tel' pero no funcionó. No tengo ningún problema si uso 'Número' o 'Tel', el problema es que acepta 'e'.

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

¿Hay alguna razón por la que no utilizas el campo sap.m.Input con el tipo "Tel"? Con esto obtendrías esto directamente (e incluso el teclado correcto en dispositivos móviles).

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?