Javascript Validation - Letters and Space - javascript

Good day. I'm very new to Javascript. I have this expression: var firstname = /^[A-Za-z]+$/; to validate first name in a form. However, this code only allows the user to input a single name without any space. How about if the user has two or more given first name?
I would like to know how to validate letters and space. I have searched similar questions here in Stackoverflow, but none of the answers worked for me.
I have tried these codes which I have found here: /^[A-Za-z ]$/ or this one /^[a-zA-Z\s]*$/ but these didn't work. Still, whenever I input two names in the First Name field, my alert message prompts me that I have entered an invalid character.
Thanks.

There is a problem with each one that you posted for what you want:
var firstname = /^[A-Za-z]+$/; should be var firstname = /^[A-Za-z ]+$/; (added space)
/^[A-Za-z ]$/ should be /^[A-Za-z ]+$/ (added +)
/^[a-zA-Z\s]*$/ is a problem because I assume from the other examples that you want at least one letter, so it should be /^[a-zA-Z\s]+$/ (*replaced * with +)

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

check input fields with 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.

how to validate a text field in jsp using java script for accepting only dot and comma as special characters

I need to allow only numerics, dot and comma in a text field. If the user enters other special characters an alert needs to be thrown.
Please find below the conditions:
The text field should contain atleast one numeric value.
It is not mandatory that the text field should contain dot and comma always.
Thanks for your help.
Here is a start for you: http://jsfiddle.net/KLyy8/1/
Unless you tell us the exact specifics we won't know how to write the regular expression. Sounds like you want numbers with thousands separator and decimals. But how many of each? The fiddle above will work for numbers like this...
123,456.79
12,345.6
1,234.56
etc...
validate=function(){
var str = document.getElementById('test').value;
var patt = new RegExp("^[0-9]{1,3}\,[0-9]{3}\.[0-9]{1,2}$");
var res = patt.test(str);
alert(res);
}
If the field should contain only numeric values with decimal places (for exmaple 3.14, 112358 or 9,81), then you can use only one regex:
function validateString(str){
if(str.match(^[0-9\.,]+$)){
return true;
}else{
return false;
}
However if you want to be precise you will probably want to limit the dots and commas to one, if I understand your situation correctly.
Regex for that can look like this
^[0-9]+(\.|,){0,1}[0-9]+$
That means it starts with a number (^[0-9]+), then it may contain zero or one times one comma or dot ((\.|,){0,1}) and then again ends with a number ([0-9]+$).

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?

Testing valid mail address using exec

I have wrote the below function to validate the email address entered by user.
function checkEmail(v_email)
{
var l_ret=true;
var l_reg = /^[a-z0-9._-]+#[a-z0-9.-]{2,}[.][a-z]{2,3}$/;
if (l_reg.exec(v_email)==null){l_ret=false;}
return l_ret;
}
It works perfectly fine.. with lowercase email address, (example.myemail#example.com) but if it detect any capital letter then it fails. like (example.MYemail#example.com).
I am trying to make it work for both capital as well as lower letter but i am not able to make it.. any one good with regex..could please suggest.
thank you in advance...
regards,
Mona..
Simply use the case insensitive mode:
/^[a-z0-9._-]+#[a-z0-9.-]{2,}[.][a-z]{2,3}$/i;
^
It makes [a-z] also match uppercase letters. Otherwise, you can use [A-Za-z] to mean both upper and lowercase characters in regex.
Noooooooooooo you DONT use regex to validate EMAIL..
An Email is valid if you can send a mail to it..
To validate Email,follow these steps..
1>Send Mail to that email address in which you can put an activation code or even a link.
2>If you receive the respose,email is valid..
At max your regex should be
^[^#]+#[^#]+$
Stop Validating Email Addresses With Complicated Regular Expressions
Change your regex like this:
var l_reg = /^[a-z0-9._-]+#[a-z0-9.-]{2,}[.][a-z]{2,3}$/i;
instead of
var l_reg = /^[a-z0-9._-]+#[a-z0-9.-]{2,}[.][a-z]{2,3}$/;
Make the regex ignore the case by adding the flag i like below:
var l_reg = /^[a-z0-9._-]+#[a-z0-9.-]{2,}[.][a-z]{2,3}$/i;

Categories

Resources