Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have tried this new code and one problem left how do I make optional plus (+) , like user only can insert in the beginning of the text. Like when you insert
0164+++1594 //false
+0123456789 // true



Google Code Archive - Long-term storage for Google Code Project Hosting.[^]

I have search this but i don't understand i think its similiar to what i want

What I have tried:

I have tried this new code and one problem left how do I make optional plus (+) , like user only can insert in the beginning of the text :-

JavaScript
$("#hpno").on("keypress keyup blur",function (event) {
            //this.value = this.value.replace(/[^0-9\.]/g,'');
     $(this).val($(this).val().replace(/[^\+?0-9]/g,''));
            if ((event.which != 43 || $(this).val().indexOf('+') != -1) && (event.which < 48 || event.which > 57)) {
                event.preventDefault();
            }
        });
Posted
Updated 27-Aug-18 19:11pm
v8
Comments
Member 13951173 26-Aug-18 22:11pm    
This is what I have search and try https://jsfiddle.net/da90nt3c/49/

Like in the text box only:
+60123456789 //true
0123456789 // true
++012345678 //false
012544++++516 // false

Like it just only allow user insert the plus once. But the plus can be optional. But can't have more than one plus
Patrice T 26-Aug-18 22:20pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Mohibur Rashid 28-Aug-18 0:20am    
You did not follow the suggestions you got for your old questions. please follow the answers. It's all there.

1 solution

Based on your current and previous posts, I'm guessing, you want the textbox to show only the phone format that matches the regular expression. I don't think the replace approach will work because it will replace the valid phone with empty string. The below code will store the valid input into a temporary variable if the input matches the regular expression. if not match, restore the textbox value with the temporary value.

Here is an example.

HTML
<input type="text" id='txtPhone'>

JavaScript
var tempPhone = '';
  $("#txtPhone").keyup(function (e) {
 
    if($(this).val().match(/^\+?\d{0,}$/))
    {
       tempPhone =  $(this).val();
   		 $(this).val( tempPhone);
    }
    else {
        $(this).val( tempPhone);
    }
	});
  
  //paste, clear the temp value
   $('#txtPhone').on('paste', function () {
    tempPhone = '';
   });


Example:
CP_phone_number - JSFiddle[^]
 
Share this answer
 
Comments
Member 13951173 28-Aug-18 2:48am    
Thank you so much, It works...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900