Home Java Javascript-array JavaScript test() method



JavaScript test() method
Posted on: November 15, 2008 at 12:00 AM
In this section, you will study the use of test() method. The test() method returns true if a pattern exists within a string, otherwise it returns false. This method searches a string for a specified value.

JavaScript test() method

     

In this section, you will study the use of test() method. The test() method returns true if a pattern exists within a string, otherwise it returns false. This method searches a string for a specified value. You can see in the given example, we have used RegExp in order to search the specified pattern in the string. The expression pattern.test(st1) tests whether the specified pattern('@') exists in the Email ID or not.

 

 

 

 

 

Here is the code:

<html>
<head>
<h2> Use of test() method</h2>
<script type="text/javascript">
var st1 = "peter@roseindia.net";
var st2 = new RegExp("@");
if (st2.test(st1))
document.write(st1 + " is a valid Email ID")
else
document.write(st1 + " is an invalid Email ID");
</script>
</head>
</html>

Output will be displayed as:

Download Source Code

 

Related Tags for JavaScript test() method:
cstringsearchiomethodtestpatternvaluereturnthisifforiestudywithexistscieareilitsectionlsusewisepeinmtrthinarcesspectruestsfalsercmesurnspatislleaarstrwisvattxissriringthstaluexiono


More Tutorials from this section

Ask Questions?    Discuss: JavaScript test() method  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.