Name validation no space - javascript

I'm trying to do a validation including these requires:
Allow to insert a name with space between , example: "Nicolas Tesla"
Not allow to insert spaces or numbers , example: " " or "12354"
I have this function but i don't know if i'm doing right:
function ValidName(){
var regex = /^[a-zA-Z]$/;
var name = $("#txtNome").val();
if(name == ""){
alert("Please, type your name.");
name.focus();
return false;
}
else if(name =! name.regex){
alert("Please, type your name.");
name.focus();
return false;
}
}
The first "if" is working , if i don't type anything then alert comes out , but the second one isn't and i don't know if i'm doing right , in fact i don't know what i'ma doing.

First of all, modify your regex so that it accepts strings with at least one character separated with one space with another set of one or more characters:
var regex = /^[a-zA-Z]+(\s[a-zA-Z]+)?$/;
That will save you the first "if" statement.
Then, to check if your regex match, you should use RegExp.prototype.test
your code would then look like
function ValidName(){
var regex = /^[a-zA-Z]+(\s[a-zA-Z]+)?$/;
var $name = $("#txtNome");
if(!regex.test($name.val())){
alert("Please, type your name.");
$name.focus();
return false;
}
return true;
}

Related

JS Form validation, invalid characters?

Trying to validate my form and set up a variable for invalid characters, but I'm having trouble getting them recognized because they're just a bunch of symbols? -
function validation(){
var Name =
document.getElementById("name").value;
var Email = document.getElementByID("email").value;
var invalidSymbol = /[\~\`\!\#\$\%\^\&\*\(\)\-\+\{\}\:\\\;\"\'\<\>\?\,\]/;
if Name == ""{
alert("Please enter your name");
document.getElementById("Name").focus();
return false;
}else if (Email == "" | | Email.indexOf("#")<1 || Email.lastIndexOf("#")+2 || Email.lastIndexOf(".")+2>=Email.indexOf("#").length || Email.match(invalidSymbol)){
alert ("Please enter a valid e-mail address");
document.getElementById("email").focus();
return false;
}else{
return true;
}
}
var desired = stringToReplace.replace(/[^\w\s]/gi, '')
As was mentioned in the comments it's easier to do this as a whitelist
- replace the characters which aren't in your safelist.
The caret (^) character is the negation of the set [...], gi say
global and case-insensitive (the latter is a bit redundant but I
wanted to mention it) and the safelist in this example is digits, word
characters, underscores (\w) and whitespace (\s).
As stated here:
javascript regexp remove all special characters
by
annakata

RegExp name validation using hexadecimal metacharacters

I want to make a regex validation using hexadecimal metacharacters. the reason i decided to go this way it because i need only the greek language to work.
from http://www.unicode.org/Public/UNIDATA/Blocks.txt i see that i need to use characters from 1F00 to 1FFF. however i cannot find what is going wrong.
here's what i've done so far:
document.querySelector("#register input[name='first_name']").onblur =
function(){
/*RegEx about name*/
var str = /[\u1F00-\u1FFF]/g;
var name = document.querySelector("#registerinput[name='first_name']").value;
if (name == null || name == ""){
alert("First name must be filled out!");
}
else if(!name.match(str)){
alert("Name must contain (greek)letters only!");
}
};
The range you are using is Greek extended. You want the range from 0370 to 03ff. From the page you quoted:
0370..03FF; Greek and Coptic
1F00..1FFF; Greek Extended
function is_greek(name){
var greek = /[\u0370-\u03ff]/;
return greek.test(name);
}
> is_greek("α")
< true

Regex - How to allow white space comma and letters

I'm trying to validate text field using JavaScript. It should allow (a-z, white space between words, and A-Z).
I tried this but it doesn't work:
function formValidator(){
var name = document.getElementById('name');
if(isAlphabet(name, "Please enter only letters for your name")){
return true;
}
return false;
}
function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z.,\b]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
I think your regex should look like this:
/^[a-z]+(\s+[a-z]+)*$/i
This allows A-Z (case-insensitive because of /i), followed by any amount of white-space characters and A-Z again. The last part may be repeated or doesn't need to be present at all.
Allow only single space between words:
/^[a-z]+( [a-z]+)*$/i.test(str);

javascript validation for non empty,number and alphabets

I have a form that accepts name, phone no. Currently, I have done validation for non empty fields. I want to add validation for name (alphabets) and phone no.(numbers).
My current code is given below. On submitting the form, I will be calling validateform() function:
function validateform()
{
var str= true;
document.getElementById("name").innerHTML="";
if(document.frm.name.value=="")
{
document.getElementById("name").innerHTML="Please enter Name";
str=false;
}
return str;
}
The value entered for name should be only alphabets. If not, I want to show the message "enter only alphabets". How should I do it?
/*mobile number of 10 digits */
var digit=10;
var pattern=/[0-9]{digit}/;
if(!pattern.match ( phoneno ) )
alert("not a valid number");
As noted in the comments, you can try something like this:
var rule = /^[a-zA-Z]*$/;
if(rule.test(document.frm.name.value)) {
// this contains only letters !
}
You could use regex to achive what you're trying to do.
function validatePhonenumber(value) {
var regexp = /^[0-9]+?$/;
return regexp.test(value);
}
function validateAlphabet(value) {
var regexp = /^[a-zA-Z ]*$/;
return regexp.test(value);
}
var alphaExp = /^[a-zA-Z]+$/;
if(!document.frm.name.match(alphaExp))
{
document.getElementById("name").innerHTML="Please enter only alphabets";
str=false;
}
var numExp = /^[0-9]+$/;
if(!document.frm.phone.match(numExp))
{
document.getElementById("phone").innerHTML="Please enter only numbers";
str=false;
}
With this you don't need to check for empty input. If you want to handle empty input separately, replace + with a * in the regexes.
function alphanumeric(inputtxt)
{
var letters = /^[0-9a-zA-Z]+$/;
if(inputtxt.value.match(letters))
{
alert('Your registration number have accepted : you can try another');
document.form1.text1.focus();
return true;
}
else
{
alert('Please input alphanumeric characters only');
return false;
}
}
See more here.

javascript for checking alphabets from a string

I have a string which is of format 245545g65.
var value = "245545g65"
var last3Letters = value.substring(7,9); // abc
Now I want to validate whether the last three letters contains only alphabets, if it is alphabet , i want to alert it.how to alert g?
how do i do this?
assuming that "contains only alphabets" means the last three characters are a combination of the letters a-z:
var str = '245545g65';
if (/[a-z]{3}$/.test(str)){
// last three characters are any combinations of the letters a-z
alert('Only letters at the end!');
}
you can use RegEx and compare length
var re = new RegExp("[^0-9]*", "g");
var newlast3Letters =last3Letters.replace(re,"");
if(newlast3Letters.length!=last3Letters.length)
{
alert("not all alphabets");
}
else
{
alert("all alphabets");
}
you can use isNaN to check weather s string is number
if (!isNan(last3Letters))
alert(last3Letters + ' is number.')
else
alert(last3Letters + ' is not number.')
You can also do this:
var value = "245545g65"
if(value.slice(value.length-3).search(/[^a-z]/) < 0) {
alert("Just alphabets");
} else {
alert("Not just alphabets");
}
Easy:
var alpha = /^[A-z]+$/;
alpha.test(last3Letters);
This will return a boolean (true/false). Stolen from here.

Categories