Reputation:
I have a form
on my website with a mobile telephone number input
.
I am using validation that allows the user to either enter:
a: 0 7725 878 811
b: +44 7725 878 811
I would like to remove the first 0 or +44 when submitting the form
so that the value that ends up in the database is simply 7725 878 811
What would be the best way to go about this?
Different to marked as duplicate as it is not validation I have an issue with, it is with cleaning the value upon submit
Upvotes: 1
Views: 2837
Reputation: 39
Your problem is only with 0 and +44?
I would put a mask on the field to force the user to type the region code, and after that treat the result.
You can either split the string by space (' '), or something like that, and remove the first ocurrency, or use regex (like already answered) but remove the plus sign and first two digits.
For instance:
value.replace(/^(0|\+\d\d) */, '')
and so on
Upvotes: 0
Reputation: 29683
To replace 0
or + any 2 digits
you can simply use below method:
var value=$('#yourcontrolid').val();
value=value.replace(/^(0|\+\d\d) */, '')
value
will have newly fetched value which you can send and store in DB. The above regex
will replace 0
or +44
,+91
etc.,
Upvotes: 0
Reputation: 3933
When handling the form, try getting the input value and removing the 0/+44 using Regex:
var inputVal = $('#yourinputid').val(); //gets the input
inputVal.replace(/^(0|\+44) */, '');//remove the 0 or +44 using regex
So handling your form before submit, should be something along the lines of this:
$("#form-id").on("submit", function(e){
e.preventDefault();
//gets the input
var inputVal = $('#yourinput-id').val();
//remove the 0 or +44 using regex
inputVal.replace(/^(0|\+44) */, '');
//re-set input value to your new value without 0 and +44
$('#yourinput-id').val(inputVal);
//write your other code
//then submit your form
this.submit();
});
Upvotes: 1
Reputation: 10069
The best way would be to attach a handler to the submit
event of the form, changing the value there and then submitting it. With JQuery it'd be something like:
$("#theForm").on("submit", function(e){
e.preventDefault();
// Modify the input value here
// ...
this.submit();
});
Upvotes: 1