Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
181
rated 0 times [  184] [ 3]  / answers: 1 / hits: 99023  / 11 Years ago, sat, july 27, 2013, 12:00:00

I have an input type=text for names in my HTML code. I need to make sure that it is a string with letters from 'a' to 'z' and 'A' to 'Z' only, along with space(s).



This is my HTML code:



<form action= name=f onsubmit=return f1()>
Name : <input type=text name=name>


I'm expecting my JavaScript to be something like this:



function f1() 
{
var x=document.f.name.value;
.....
.....
return false;
}


PS: I'm not really familiar with Regular Expressions, so please do put up an explanation with the code.


More From » html

 Answers
175

You can use javascript test() method to validate name field. The test() method tests for a match in a string.



/^[A-Za-zs]+$/.test(x) //returns true if matched, vaidates for a-z and A-Z and white space


or



/^[A-Za-z ]+$/.test(x)

[#76698] Friday, July 26, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kennedim

Total Points: 699
Total Questions: 85
Total Answers: 105

Location: Saint Helena
Member since Mon, Jan 16, 2023
1 Year ago
;