Multiple type Phone Number format javascript in PDF - javascript

I'm using NitroPDF to make custom forms and I have a problem....
Although NitroPDF allow me to format numbers using Arbitraty Mask, it only allow to use one custom pattern. But in my country, landline numbers have less numbers than cellphone numbers.
For example:
Landline: (99) 9999-9999
Cellphone: (99) 99999-9999
How could I let NitroPDF identify and apply mask based on field size, using javascript?
I don't know if this Arbitraty Mask allow me to do something like:
(99) 9999-9999 or (99) 99999-9999
Can someone help me? :)

Could you just specify the amount of digits you want with a limiting range?
(\(\d{2}\) \d{4,5}-\d{4})
So this looks for exactly two digits inside of parenthesis, followed by a space. Then it checks for either four or five digits. Finally, it looks for a dash followed by exactly four more digits.
Here is a demo

Related

Lat/Long mask positive/negative detection

Hey all I am trying to create a mask for my 2 input boxes that will house the latitude and longitude.
Taken that the latitude can be a positive or negative number and the same goes for longitude. The issue being is that I am trying to come up with a regex that can give the user the ability to first chose if its going to be a positive or negative number then give user the mask of (-/+)XXX.XXXXXX.
However, I am not getting too close to the conclusion and so I am asking for a some help in reaching that goal.
I have set up a JSFiddle of what I have so far, which is not much, in order to archive my goal.
$('.mask').inputmask({
regex: String.raw `^[-/*][0-9]{3}[.][0-9]{7}$`
});
The above code works for negative numbers but when typing out, say 100., it just sits there because its waiting on the - in order to begin. I thought adding the * it would allow it to input a positive number. The above should allow the user to input in the format of -XXX.XXXXXX OR XXX.XXXXXXX.
I still need to figure out how to also tell when the first part (xxx.) is only 1 or 2 digits instead of 3 which makes the user have to put in a 0's in front of said number digits.
So valid input would be like so:
-2.984593 instead of -002.984593
-74.192822 instead of -074.192822
-102.738631
7.653721 instead of +007.653721
10.746633 instead of +010.746633
110.938365
How can this be formatted in order to work as I need it to?
You could simply make the dash optional:
^-?\d{3}\.\d{7}$
I took the liberty of refactoring your RegEx pattern ([0-9] -> \d, [.] -> \.).
As for allowing the first part to be one or two digits, you could use:
^-?\d{1,3}\.\d{7}$

regex check for Float number -> not ending and stars with decimal point

I am facing issues with regex pattern for Float number -> that should not end or stars with decimal points..
I have tried following regex patter.. that is
regex = /^\d*\.?\d*$/
// on doing
regex.test(11.)
regex.test(.11)
// it is returning true in checking
// I need to make this as false, comment will be much helpful
thank you.
You should bear in mind that regex only works with strings. When you pass a non-string variable as input to a RegExp, it will first coerce it to a string type.
Have a look:
console.log(11. , 'and', .11); // => 11 and 0.11
So, the actual string values you pass to your ^\d*\.?\d*$ regex are 11 and 0.11 that can be matched with the given pattern. Actually, ^\d*\.?\d*$ is a regex that is usually used for a very loose live number input validation, e.g. see How to make proper Input validation with regex?.
What you want is to implement a final, on-submit validation pattern, so that it could not pass strings like 11. and .11. There have been lots of threads discussing this kind of regex:
Regular expression for floating point numbers
regular expression for finding decimal/float numbers?
Basically, for validation, you will need something like
/^\d+(?:\.\d+)?$/.test(input_string)
/^[0-9]+(?:\.[0-9]+)?$/.test(input_string)
/^[0-9]+(?:\.[0-9]{1,2})?$/.test(input_string) // Some need to only allow 1 or 2 fractional digits
/^[0-9]{1,3}(?:\.[0-9]{2})?$/.test(input_string) // 1-3 digits in the integer part and two required in the fractional part

How to validate mobile number for countries in angular?

Here I validate mobile number using below code for universally for all countries using below code using this regex (/^(\+\d{1,3}[- ]?)?\d{10}$/) I doing but it not accept this type numbers ((+91)9876543210 OR 09876543210)
This type of mobile Numbers accept
+91 9876543210 (or all other country code)
(+91) 9876543210
(+91)9876543210
+919876543210
+91-9876543210
(+91)-9876543210
9876543210
09876543210
not accept numbers like
++91 9876543210(double ++),
+91 9876543210(double spaces),
+91 987 65 7 4 688(not more than one space),
+91 808-75-74-678(not more than one -),
+91-846363,
80873(number less than 10 digit),
8087339090456(number greater than 10 digit),
0000000000(all zeros),
+91 0000000(all zeros with country code)
HTML
<mat-form-field>
<input matInput placeholder="Enter contact No." [(ngModel)]="userObj.contactno" name="contactNo" required>
</mat-form-field>
<button mat-button color="primary" (click)="saveNewCustomerDetails()">SAVE</button>
TS
saveNewCustomerDetails(){
if(this.userObj.contactno.match(/^(\+\d{1,3}[- ]?)?\d{10}$/)){
console.log("valid mobile number");
}else{
console.log("Invalid mobile number");
}
}
You need to incorporate literal parenthesis in your regex which can be there optionally and since they have special meaning hence you need to escape them in your regex.
Seeing your last mobile number 09876543210, it seems you optionally want to support zero before a number if there is no country code, so you can have zero in alternation for your country code part.
And last, like you don't want to support a number only having all zeroes, you can have a negative look ahead (?!0+$) in your regex. Your updated regex becomes,
^(?!0+$)(?:\(?\+\d{1,3}\)?[- ]?|0)?\d{10}$
Live Demo
Also, you if you don't want to support numbers having only same digits like 2222222222 or 5555555555 then your negative look ahead needs to be written as (?!(\d)\1+$) and your regex will become,
^(?!(\d)\1+$)(?:\(?\+\d{1,3}\)?[- ]?|0)?\d{10}$
Demo for this updated regex
Another way for validating mobile numbers can be, you replace everything that is not a digit and also get rid of all leading zeroes in your number, then what remains will be pure mobile number which you can further validate. But there may be certain disadvantages there as it may become a bit hard to figure out the country code and if the number is really valid. Because certain numbers may be less than ten digits and may actually be invalid but they may be valid if they belong to a country like Singapore which has I guess 8 digit numbers. And same with countries having eleven digits local number might trouble you.
Your pattern is looking correct don't know why it is rejecting, can you try once with
^(\+[\d]{1,5}|0)?[7-9]\d{9}$
Maybe it will work
You can try this..
^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[789]\d{9}$
This is work for me.

angularjs convert 0 to 1 in number (html5) field

its an odd behavior. m using input field field where type is number
and if i enter 1230 model value remains -> 1230
but as i type 01 its becomes -> 1
where as i can see 01 in input value . so this something to do with angular js
i need 00 in model because its user phone number and number type is to stop user from entering text
any help will be appreciated
https://docs.angularjs.org/api/ng/input/input%5Bnumber%5D
test can be run at angular site
The type number allows you to enter +-,.. So you cannot achieve your goal of preventing the user from entering "invalid" numbers in the first place.
It's also not very user friendly, as telephone numbers are often formatted using spaces and braces. A user can no longer copy and paste such values into the input field. Digits in phone numbers can also be represented by letters btw. 123-HELLO is equal to 12343556.
Please note that there's also an input type tel. It's not particularly useful but semantically more appropriate.
If you only want to save and display the value then use the input as is. It doesn't make much sense to force a user to adhere to your preferred pattern. Adding pattern or ngPattern allows you to use regular expressions to limit the possible characters. If you need the plain number then strip all non-numeric characters - and possibly convert roman letters to numbers - before usage.

Javascript Regular Expression for International Phone Number

The following regular expression isn't working for international phone numbers that can allow up to 15 digits:
^[a-zA-Z0-9-().\s]{10,15}$
What needs to be adjusted?
You may find the following regex more useful, it basically first strips all valid special characters which an international phone number can contain (spaces, parens, +, -, ., ext) and then counts if there are at least 7 digits (minimum length for a valid local number).
function isValidPhonenumber(value) {
return (/^\d{7,}$/).test(value.replace(/[\s()+\-\.]|ext/gi, ''));
}
Try adding a backslash:
var unrealisticPhoneNumberRegex = /^[a-zA-Z0-9\-().\s]{10,15}$/;
Now it's still not very useful because you allow an arbitrary number of punctuation characters too. Really, validating a phone number like this — especially if you want it to really work for all possible international phone numbers — is probably a hopeless task. I suggest you go with what #BalusC suggests.
See A comprehensive regex for phone number validation and Is there a standard for storing normalized phone numbers in a database?
and then counts if there are at least 7 digits (minimum length for a valid local number).
The shortest local numbers anywhere in the world are only two or three digits long.
There are many countries without area codes.
There are several well-known places with a 3 digit country code and 4 digit local numbers.
It may be prudent to drop your limit to 6 or 5; just in case.

Categories

Resources