Click here to Skip to main content
13,766,364 members
Rate this:
 
Please Sign up or sign in to vote.
How do I make like country code +601 or 01 like make country code static in the front,and make + character optional and don't allow user to insert alphabet. Example +60123456789 or 0123456789. The bold one need to be in the front see as if user can insert either one.

What I have tried:

This is what i had try but it only validate that allow only number and + character ,but the character + can insert it anywhere.
$(function(){
$('#hpno').keypress(function(e){
var txt = String.fromCharCode(e.which);
console.log(txt + ' : ' + e.which);
if(!txt.match(/[+0-9]/))
{
return false;
}
});
});
Posted 20-Aug-18 3:32am
Updated 20-Aug-18 11:33am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You have to specify the + character first being optional followed by digits only and matching the whole input:
if(!txt.match(/^\+?[0-9]+$/))
Explained in detail:
^ Begin of string
\+ The + character (must be escaped with a backslash here)
? Matches the preceeding zero or one time
[0-9] Digits only
+ Matches the preceeding one or more times
$ End of string

An additional option might be limiting the number of digits to a minimum and maximum (here min. 6 and max. 16):
if(!txt.match(/^\+?[0-9]{6,16}$/))

I suggest to read about regular expressions. For JavaScript you might start at RegExp - JavaScript | MDN[^].
  Permalink  
Comments
Member 13951173 20-Aug-18 21:24pm
   
I have try both codes in my coding the first one it can't insert + character an the second example that you gave everything won't allow even numbers....:(
Jochen Arndt 21-Aug-18 3:04am
   
The problem is not the regex but where you call it. My regex is for checking a complete input string.

I overlooked that you are using it within a keypress handler. That is not really useful for such checks because you can handle only single characters. While you can check those for being a + sign or a digit, you can't check if the actual keypress is the first character or not.

You might let your initial code in place to catch unallowed characters (which can be simpler and faster performed with an IF condition checking if e.what() is a digit or the + sign) and add a handler to check the whole input string. There you can use the regex from my solution.
Member 13951173 21-Aug-18 3:28am
   
Means like this????


jQuery(function () {
$("#hpno").keyup(function () {
var VAL = this.value;

var phone = new RegExp(/^\+?[0-9]+$/);

if (phone.test(VAL)) {
alert('Great, you entered Phone Num');
}
});
});
Jochen Arndt 21-Aug-18 4:21am
   
No. That is again for a single key.

The common method is to implement a validation function fo the whole form or for each field.

Example for a form field:

$('#hpno').on('input', function() {
 var input=$(this);
 // Apply regex to input here and act accordingly
});
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

[+0-9]

the RegEx only match 1 char, either a '+' or a digit. You should study RegEx more.

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx:
Regexper[^]
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web05-2016 | 2.8.181114.1 | Last Updated 23 Aug 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100