Where is the error in Javascript?

View the console output with the display of running event in Yandex.The metric https://karedesign.ee/ru/product/box-zebra?_ym_debug=1

If you click "add To cart" and click on the "Continue" button, it runs the event handler:
$("#telefon-submit").click(function(event){

Next is checking the input data:
if(/^(?=.*[0-9])[0-9 \(\)\+-]+$/i.test(document.getElementById('phone').value))
submitForm();
else
 $("#phone").css("border", "2px solid red");


If nothing to enter or to enter numbers, why not simultaneously trigger both conditions TRUE and False?
There submitForm(); is triggered and Yandex Metrika receives the event
yaCounter45870459.reachGoal('add-telefon');
and at the same time the fields highlighted in red?
$("#phone").css("border", "2px solid red");

Here is the piece of code in question:
the <script>
$("#telefon-submit").click(function(event){
 // cancels the form submission
event.preventDefault();
 if(/^(?=.*[0-9])[0-9 \(\)\+-]+$/i.test(document.getElementById('phone').value))
submitForm();
else
 $("#phone").css("border", "2px solid red");
});
 function submitForm(){
 // Variables with data from the form
 var phone = $("#phone").val();

$.ajax({
 type: "POST",
 url: "sendmail.php",
 data: "phone="+ phone + "&product="+ location.href
 success : function(text){
 if(text == "success"){
formSuccess();
}
}
});
}
 function formSuccess(){
 yaCounter45870459.reachGoal('add-phone'); //Yandex Metrika
}
 </script>
June 10th 19 at 16:33
2 answers
June 10th 19 at 16:35
Solution
Of course, since /^(?=.*)/ finds the beginning of any line that would be for him not followed.
If you do /^(?=.+)/ - an empty value is not atlavitsa.
introduced
if(/^(?=.+)/i.test(document.getElementById('phone').value)){


still runs and TRUE and False conditions... let's see ... I don't understand why.. - christina commented on June 10th 19 at 16:38
The TRUE condition is not fulfilled!!

I removed the function formSuccess(), but yaCounter45870459.reachGoal('add-telefon'); this event is still triggered...
Can we determine where it runs??
In tagmanager not. - christina commented on June 10th 19 at 16:41
Nonsense, just checked the console:
/^(?=.+)/i.test('+7978')
true
/^(?=.+)/i.test(")
false
- rhianna_Fee commented on June 10th 19 at 16:44
AA tin! Found the problem.
I have previously this script was performed using Tagmanager. Then I deleted it from there, but the changes are not pressed "Confirm", so I still hung the old version of the Tagmanager with this script - in this case the tag with this script already anywhere was not - it was removed but not CONFIRMED!

problem solved - thank you! - christina commented on June 10th 19 at 16:47
how complicated =)
To your health! - rhianna_Fee commented on June 10th 19 at 16:50
June 10th 19 at 16:37
so through sources in dev tools put breakpoint in the beginning of the function and see the steps as to what is considered, in chrome the easiest

I would first of all put a check on the existence of values generally, and then the rest of the combine would be charged

the rally will not understand your regex, it checks for an empty field?
like this regex should check for any records
12345678
(+372)1234567889
+372123456

that is only numbers, spaces, parentheses, the plus sign and that is not it was empty.
can wrong? - christina commented on June 10th 19 at 16:40
it may be easier to do explicit checks for nothing? - christina commented on June 10th 19 at 16:43

Find more questions by tags JavaScript