Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is my HTML code which has an input type = “text” for names. 

<form action="" name="f" onsubmit="return f1()">

                Name : <input type="text" name="name">

This is my expected JavaScript: 

function f1() 

{  

   var x=document.f.name.value;  

   ..... 

   ..... 

   return false;

}

Can anyone tell me how to get the input string with letters from 'a' to 'z' and 'A' to 'Z' along with space(s) with the expected JavaScript?

1 Answer

0 votes
by (19.7k points)

The test() method in JavaScript tests for a match in a string which will validate your name field. Check the below code implementation: 

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

or

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

If you want to learn more about Javathen go through this Java tutorial by Intellipaat for more insights. 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...