Form validation name input issue with whitespace? [duplicate] - javascript

This question already has answers here:
Regular expression for first and last name
(28 answers)
Closed 2 years ago.
So I have this regex to confirm if a name is valid or not, but if I were to add spaces before the name, it will display my error message.
Is there a way to convert my code to validate names like " Bob Joe" if there's extra spaces in the front? But I also wanna make sure nobody can just type "a b joe b bob" and still get a valid name.
Here's my current validation code
if (!values.name) {
errors.name = 'Name required';
} else if (!/^[A-Za-z]+/.test(values.name)) {
errors.name = 'Enter a valid name';
}

Please try string.trim() function to validate your input.Please click link to better understand https://www.w3schools.com/jsref/jsref_trim_string.asp#:~:text=The%20trim()%20method%20removes,not%20change%20the%20original%20string.

Related

Regex evaluate to true but its not supposed to [duplicate]

This question already has answers here:
Match exact string
(3 answers)
Closed 5 years ago.
I have input field which db is expecting to come as integer. if i type 60 it works fine, but 60.00 doesn't. I did RegExp for validation with the ^[0-9]+/ expession. It works fine for inputs like 60.asdass, 60.0320320, dasdasdas.60 etc. but if i type 60. and it evaluates to true and it passes the validation and i get an error from db. How can i make my regex to sets as false in this situation?
Add the end of string anchor ($):
^[0-9]+$
You could test the rest of the array as well and use start and end of the string as well for testing.
function check() {
var element =document.getElementById('input'),
value = element.value;
element.style.color = /^\d+(\.\d+)*$/.test(value) ? '#000000': '#ff0000';
}
<input id="input" type="text" onkeyup="check()">
You can use this. By using anchors it will fail if the full input is not the regular expression specified.
^\d+$

Why the name validation is invalid? [duplicate]

This question already has answers here:
Reference - What does this regex mean?
(1 answer)
Amend Regular Expression to allow German umlauts, French accents and other valid European letters
(4 answers)
Closed 6 years ago.
Why the name is not valid when user is type:
André
René
François
Anne-Marie
CODE:
var myNameFilter = /^([a-zA-Z ]+)$/;
var a = $('#firstname').val();
if(a=='') {
alert("You stupid your name is empty. Fill it in NOW!!!");
return false;
} else if (!myNameFilter.test(a)) {
alert("Are you stupid? how can you type your name wrong. you idiot. fix it, dont call me for this.");
return false;
}
[a-zA-Z] detect only english. they contain unicode characters and so it will not detect it.
Now check it
var a="André,René,Fran çois,Anne-Marie,éç,é-çé ç,Éric,Hélène".split(",");
var myNameFilter = /^([a-zA-Z\-éçèàùâêîôûëïüÿçÉ\s]+)$/;
i=0;
while(i<a.length){
console.log(a[i]+"->"+myNameFilter.test(a[i]));
i++;
}

I want to extract all the characters before "#" symbol in an email address entered by customer in html page [duplicate]

This question already has answers here:
How can I extract the user name from an email address using javascript?
(9 answers)
Closed 6 years ago.
I want to extract all the characters before "#" symbol in an email address entered by customer in html page. I do not know which function to use in java script to extract the data and assign it to a variable. i tried substring method but it is only displaying the position of the character "#". open for help and suggestions. Thanks in advance.
var email = "some.body#domain.com";
var name = email.substring(0,email.indexOf("#"));
console.log(name);
Maybe try this:
var email = document.getElementById("YourElementId").value;
var before = email.split("#")[0];
console.log(before);

Use javascript to determine email address is valid or not [duplicate]

This question already has answers here:
How can I validate an email address in JavaScript?
(79 answers)
Closed 6 years ago.
I have 6 email address like below:
(1) ali#hotmail.com -> valid
(2) #hotmail.com -> invalid
(3) ali#hotmail.com, abu#hotmail.com -> valid
(4) ali#hotmail.com, #hotmail.com -> invalid
(5) ali#hotmail.com, abu#hotmail.com, ahmad#hotmail.com -> valid
(6) ali#hotmail.com, #hotmail.com, ahmad#hotmail.com -> invalid
How do I use JavaScript to determine that the email address is in full format?
I try startsWith("#hotmail.com"), endsWith("hotmail.com"), indexOf("#hotmail.com") also cannot fulfill all the email addresses above.
Can someone help me?
Using regular expressions is probably the best way. Pass the string in this function and this function will return true of false
function validateEmail(email) {
var re = /^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)| (".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
You could use regex
For example,
/[A-Za-z0-9-]+#hotmail.com/

Validation in javascript not working [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 8 years ago.
The below code is for validating email. I am getting false for inputs like,
mike#gmail.com
kid#gmail.com
stain#yahoo.com
Can someone point what mistake in the code?
function validate(){
fieldValue = document.getElementById("check").value;
pattern = new RegExp(/^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,6}$/);
if(pattern.test(fieldValue)){
alert("true");
} else {
alert("false");
}
}
Thanks
A-Z only checks capital letters. Add also a-z:
[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,6}
Using RegEx to validate email addresses is difficult.
However, the issue with your code is the casing (as others have pointed out). You can fix it by changing A-Z to A-Za-z, which will check for lowercase and capital letters.
function validate(){
fieldValue = document.getElementById("check").value;
pattern = new RegExp(/^[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$/);
if(pattern.test(fieldValue)){
alert("true");
} else {
alert("false");
}
}
For validating email addresses, this pattern has worked for me for quite some time:
/^([\w-\.]+#([\w-]+\.)+[\w-]{2,4})?$/

Categories

Resources