Regex - How to allow white space comma and letters - javascript

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);

Related

javaScript check for at least one alphanumeric character in string

In my application someone can submit text from another language. I'd like for only English alphanumeric strings to be entered. I've got this JavaScript function working but wondering if this is the best method for doing this?
var string = $('input[name=title]').val();
if((/\d/.test(string) || /[a-zA-Z]/.test(string)) === false) {
alert('Field has no Alphanumeric characters.');
fPass = false;
}
Even if some just enters 1 character I want that to be allowed, as long as it is a number or a character from one of the 26 letters in the English alphabet (case insensitive)
Without using a function here is what I've come up with
if((/[\da-z]/i.test(string)) === false) {
alert('Please use alphanumeric characters.')
}
You can combine your regex into one expression.
function hasAlphanumeric(str) {
return /\d|[A-z]/.test(str)
}
You can use
^[\da-z]+$
let allowAlphanumeric = (str) =>{
return /^[\da-z]+$/i.test(str)
}
console.log(allowAlphanumeric('$##'))
console.log(allowAlphanumeric(''))
console.log(allowAlphanumeric(' '))
console.log(allowAlphanumeric('abchbchdb12e44'))

Name validation no space

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;
}

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

JS: regex for numbers and spaces?

I'm using happyJS and use the regex underneath for phone validation
phone: function (val) {
return /^(?:[0-9]+$)/.test(val);
}
However this ONLY allows numbers. I want the user to be able to enter spaces as well like
238 238 45383
Any idea why return /^(?:[0-9 ]+$)/.test(val); is not doing the trick?
This is my suggested solution:
/^(?=.*\d)[\d ]+$/.test(val)
The (?=.*\d) asserts that there is at least one digit in the input. Otherwise, an input with only blank spaces can match.
Note that this doesn't put any constraint on the number of digits (only makes sure there are at least 1 digit), or where the space should appear in the input.
Try
phone: function (val) {
return /^(\s*[0-9]+\s*)+$/.test(val);
}
At least one number must be present for the above to succeed but please have a look at the
regex example here
Try
/^[\d ]*$/.test("238 238 45383")
console.log(/^[\d ]*$/.test("238 238 45383"));
You can try the below regex for checking numbers and spaces.
function isTextAndNumberSpaceOnly(text) {
var regex = /^[0-9 ]+$/;
if (regex.test(text)) {
return true;
} else {
return false;
}
}
Personally I use this code and it works properly:
function validateMobile(mob)
{
var re = /^09[0-9]{9}$/
if(mob.match(re))
return true;
else
return false;
}

alphanumeric regex javascript

I am having a problem to get the simple reges for alphanumeric chars only work in javascript :
var validateCustomArea = function () {
cString = customArea.val();
var patt=/[0-9a-zA-Z]/;
if(patt.test(cString)){
console.log("valid");
}else{
console.log("invalid");
}
}
I am checking the text field value after keyup events from jquery but the results are not expected, I only want alphanumeric charachters to be in the string
This regex:
/[0-9a-zA-Z]/
will match any string that contains at least one alphanumeric character. I think you're looking for this:
/^[0-9a-zA-Z]+$/
/^[0-9a-zA-Z]*$/ /* If you want to allow "empty" through */
Or possibly this:
var string = $.trim(customArea.val());
var patt = /[^0-9a-z]/i;
if(patt.test(string))
console.log('invalid');
else
console.log('valid');
Your function only checks one character (/[0-9a-zA-Z]/ means one character within any of the ranges 0-9, a-z, or A-Z), but reads in the whole input field text. You would need to either loop this or check all characters in the string by saying something like /^[0-9a-zA-Z]*$/. I suggest the latter.
I fixed it this way
var validateCustomArea = function () {
cString = customArea.val();
console.log(cString)
var patt=/[^0-9a-zA-Z]/
if(!cString.match(patt)){
console.log("valid");
}else{
console.log("invalid");
}
}
I needed to negate the regex

Categories