check input fields with javascript - javascript

i am using the following code to check if user doesnt input special characters in name
var regex = /^[a-zA-Z ]{2,50}$/;//check for only letters
var ctrl = document.getElementById('input1');//given name field
if(!regex.test(ctrl.value))
{
alert('Ensure field Name contains only letters[A-Z]');
return;
}
can someone please help to change regex so client can also enter JUST one (')(IF NEEDED) e.g O'Daniel.
also for phone no field limit user with only one +
Thanks in advance.

You don't have to cram all the logic into a single regexp. To check for apostrophes, simply do a separate check:
var apostrophes = ctrl.value.match(/'/g);
return !apostrophes || apostrophes.length < = 1;
Use the same logic to check for no more than one + in the phone number.

Related

Validating name in visual studio code

var userName = input.question('Please enter your name: '); //Asking User to enter their Name.
while (userName.includes('.')) {
console.log ("Invalid Name!");
var userName = input.question('Please enter your name: '); //Asking User to enter their Name.
}
Above code will ask the user his/her name and store it in "userName". Then it will validate using .includes to check unwanted characters and numbers.
I want to validate if userName has numbers or unwanted characters such as "?/.,;'[]{}|&^%#" etc. I have tried using .includes and validate if a name has "." However, I'm not sure how to go about from there to validate the rest.
After the while checks that it contains the unwanted characters, it will re-prompt the user to enter a new name and it will check again until it returns false.
Is there a solution to this?
You can use REGEX to search for non-alphabetic or space characters in the string:
userName.search(/^[a-zA-Z\s]+$/)
The response will be 0 or -1. 0 means that no characters except A-Z, a-z and space were found, -1 means the contrary.
Edit: I found similar question with more detailed answers here.
let chars = /[$&+,:;=?##|'<>.^*()%!-]/g;
chars.test(UserName)
Use Regular expressions ( Regex )
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

Validation to check if the user has entered the correct format

I have a dynamic grid that has a numeric field in which I have applied masking. Also it has a plus button means the user can add many rows as he wants. The masking is applied like this:
<input type='textbox' placeholder='00000-00-000' data-mask='00000-00-000'
This masking is applied when the user enters 10 digit number but it lets the user enter a 2 or 3 digit number as well. For this I am trying to apply validation while saving so that it checks whether the value entered matches the required format.
What I have done so far is :
value = $(this).find('td:eq(1)').find('input').val(); //saves the entered value in a variable value
myRegExp = new RegExp(/\d{5}-\d{2}-\d{3}/);
if (!myRegExp.test(value)) {
valid = false;
}
else
valid = true;
The value that user enters is saved in varaible called value and then I have defined my Regex Expression to match my value with that expression but somehow this regex expression is not working. Is this the correct way to define regex expression? What am I missing here? My required format is 00000-00-000. Any help would be appreciated.
Your logic is correct but you have not defined the end point that's why it allows to insert more values.
In your Regex it only checks if the 10 digits are in the specific order
try out this
myRegExp = new RegExp(/^\d{5}-\d{2}-\d{3}$/);

javascript validation not working for only alphabetic values

I have used a javascript validation function to validate it. I've used it to see whether the text entered to the html textbox is alphabetic(No numeric characters allowed). The function is called during onkeyup and onblur. The only problem is even when numeric values or special characters are typed in the validation doesn't work. If I leave the field blank then it works(Displays that the field is left blank). Here's my javascript code:
function isAlphabetic(x,y){
var exp = /^[a-zA-Z]+$/;
var a = document.getElementById(y).value;
if(a=="" || a== null){
document.getElementById(x).innerHTML = "You cannot leave this feild empty";
return;
}
else if(a!="" && a!= null){
if(y.match(exp)){
document.getElementById(x).innerHTML = "";
return;
}
else{
document.getElementById(x).innerHTML = "Only enter alphabetic characters allowed";
return;
}
}
else{
return;
}
If you use y as an id of element, I suppose you shouldn't check it with your regexp. Instead you should check a:
if(a.match(exp)) {
You don't need JavaScript anymore for any of this. Use the pattern attribute on the input field and the browser won't let the user enter anything that doesn't match, and use required to prevent submitting the form with an empty value.
Also, do you really want only ASCII letters? (are spaces allowed? how about non-ASCII letters such as "é"?)

regexp - first and last name javaScript

I'm trying to make a JavaScript regexp such as the facebook uses for real names:
Names can’t include:
Symbols
numbers
unusual capitalization
repeating characters or punctuation
source: Facebook help center
Here is my regexp:
/^[a-z \,\.\'\-]+$/i
The problem with this regexp is that it doesn't check for repeated characters or punctuation:
then I found this :
/(.)\1/
so I'm now checking it like this:
$('input [type=text]).keyup(function(){
var name = $(this).val();
var myregex = /^[a-z\,\.\'\-]+$/i
var duplicate = /(.)\1/
if(name != myregex.exec(name) || name == /(.)\1/)
{// the name entered is wrong
}
else
//the name is ok
but the problem I'm facing is with inputs like:
Moore
Callie
Maggie
what can I do to get the problem solved?
You should stop trying to solve this problem:
It is very complicated
Names are very personal
For instance your system will never be able to validate names from China
or Japan.... (For instance: Børre Ørevål ,汉/漢 )
So just leave the whole idea, and let people freely enter their names with no restrictions.
Related: Regular expression for validating names and surnames?

Validating PDF Form Fields for number characters only

Developing a PDF form that requires field validation. I am using Bluebeam however from what I see so far it works exactly the same as any other PDF form creator.
So I am trying to validate the field for number characters only and accept the value if it is number characters (must also accept text starting with single and multiple 0's). The following was having a guess of what it might have to use.
var fld = this.getfield(“Text1”)’
If IsNumeric(“Text1”) == true;
event.value = fld.value
else
alert(“Error message”)
Try this hope this helps:
var fld = this.getfield(“Text1”).value;
if(fld.match(/^\d+$/)){
event.value = fld.value;}
else{
alert(“Error message”);}

Categories

Resources