cancel
Showing results for 
Search instead for 
Did you mean: 

I want to validate phone number field?

0 Kudos
754

I have a phone number field in my application and I want to validate it. I am validating it with the regex but it's taking 'e' letter as input .

Can you help me with the regex that doesn't accept 'e'.

Here's what I have tried.

View.xml

<Label text="Phone *" />

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

placeholder="Enter your 10 digit mobile number" 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("Please provide valid information " );

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

this.temp = false;

}

return this.temp;

},

Margot
Product and Topic Expert
Product and Topic Expert
0 Kudos

Any reason why you do not use the sap.m.Input field with type "Tel"? With this you would get this out of the box (and even the correct keyboard on mobile)?

0 Kudos

I tried using 'Tel' but it didn't help. I don't have any problem if I use 'Number' or 'Tel' the problem is it's accepting 'e'.

Accepted Solutions (1)

Accepted Solutions (1)

manju537449
Participant

Hi Tanmay,

Try using below code.

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

placeholder="Enter your 10 digit mobile number" enabled="true" />

JS:

onValidate: function(oEvent) {

var oSource = oEvent.getSource();

var val = oSource.getValue();

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

oSource.setValue(val);

},

Regards,

Manjunatha

0 Kudos

Hi, Thanks manju537449 , this was such an easy solution to replace 'e' it with blank. Never thought about this.

Answers (0)