• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » JScript » String » JavaScript String indexOf() Method
Next →
← Prev

JavaScript String indexOf() Method

By Dinesh Thakur

JavaScript IndexOf() method Performs a case-sensitive search and returns the indexof the first occurrence of the specified substring in the calling String object argument, starting the search at the beginning of the string.

If the character or string isn’t found in the string value, a value of –1 is returned. by JavaScript IndexOf Method A string contained inside another is usually termed a substring. They are useful when you have a string of information, but only want a small part of it.
Optionally, you can specify where in the main string the search should begin but the returned value is always relative to the very first character of the main string by JavaScript IndexOf Method. Such as all string object methods, index values start their count with 0. An example will surely clear things with example of JavaScript IndexOf Method:

var line = "the quick brown fox jumps over the lazy dog"; 
alert( line.indexOf("the") ); // 1 
alert( line.indexOf("the", 6) ); // 32 

You can pass a second parameter as the starting point, if you don’t want to start searching at the beginning of your stringin JavaScript IndexOf Method. Remember, these indices are zero-based just like the indices of an array which means that the first letter of the string is indexed 0, the second is 1, and so on. If the substring you searched for doesn’t exist, you’ll get -1 back.The general syntax of this JavaScript IndexOf Method is:

stringName.indexOf(searchValue) 
stringName.indexOf(searchValue, [fromIndex]) 

Note that from Index must be an integer between 0 and the string’s length minus 1. search Value does not have to be a string.
In our another example we can Validating an E-mail Addresses with the help of a String JavaScript IndexOf Method.
When validating information on a form, you may want to test if the text in a text field conforms to a format of a valid e-mail address. The JavaScript IndexOf Method of validating an e-mail address that is used applies the following logic.
• Check if the e-mail address is empty; if it is, the field is not valid.
• Check for illegal characters, and if they occur, the field is not valid.
• Check if the @ symbol is missing; if it is, the field is not valid.
• Check for the occurrence of a dot; if there is none, the field isn’t valid.
• Otherwise, the field is valid.
The following steps create a form with a single field for entering an e-mail address. When the user submits the form, the field is validated prior to submission. If validation fails, the user is informed and submission is canceled.
1. In the header of a new HTML document, create a script block containing the function checkEmail that receives a text string.
2. In the function, check if the e-mail address has no length, and if it does, inform the user and return false from the function.
3. Next, check if the following illegal characters exist: /, :, ,, or ;. If any of these characters exist, inform the user and return false.
4. Next, check if the @ symbol exists. If not, inform the user and return false.
5. Now check if a dot exists. If not, inform the user and return false:
6. Finally, return true from the function if the e-mail address passed all the tests so that the complete function looks like.
7. Create another function named checkForm that takes a form object as an argument. The function should call checkEmail and pass it the value of the field containing the e-mail address and then return the result returned by the checkEmail function:

function checkForm(formObj) { return checkEmail(formObj.myField.value); }

8. In the body of the document, create a form that contains a field for entering the e-mail address and uses the onSubmit event handler to call the checkForm function:

<body> 
<form name="myForm" action="target.html" onSubmit="return checkForm(this);"> 
   E-mail: <input type="text" name="myField"><br> 
     <input type="submit"> 
   </form> 
</body> 
function checkEmail(email) 
{ 
   if (email.length == 0) { 
     window.alert("You must provide an e-mail address."); 
     return false;} 
if (email.indexOf("/") > -1) { 
     window.alert("E-mail address has invalid character: /"); 
     return false;} 
if (email.indexOf(":") > -1) { 
     window.alert("E-mail address has invalid character: :"); 
     return false;} 
if (email.indexOf(",") > -1) { 
     window.alert("E-mail address has invalid character: ,"); 
     return false;} 
if (email.indexOf(";") > -1) { 
     window.alert("E-mail address has invalid character: ;"); 
     return false;} 
if (email.indexOf("@") < 0) { 
     window.alert("E-mail address is missing @"); 
     return false;} 
if (email.indexOf("\.") < 0) { 
     window.alert("E-mail address is missing ."); 
     return false;}return true;} 

9. Save the file with the name target.html, and open it in a browser.
10. Try to submit the form without a valid e-mail address and you should see an appropriate error message.

 

You’ll also like:

  1. JavaScript String substr() Method
  2. JavaScript String split() Method
  3. JavaScript String search() Method
  4. JavaScript String match() Method
  5. JavaScript String replace() Method
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

JavaScript Tutorials

JavaScript Tutorials

  • JavaScript - Home
  • JavaScript - Operators
  • JavaScript - Comments
  • JavaScript - If...Else
  • JavaScript - For Loop
  • JavaScript - Popup Boxes
  • JavaScript - Switch Statement
  • JavaScript - while Loop
  • JavaScript - Functions
  • JavaScript - Variables
  • JavaScript - Statements
  • JavaScript - Substring()
  • JavaScript - indexOf()
  • JavaScript - Match()
  • JavaScript - Replace()
  • JavaScript - Search()
  • JavaScript - Split()
  • JavaScript - Substr()
  • JavaScript - Blink()
  • JavaScript - Anchor()
  • JavaScript - Big()
  • JavaScript - Strings Object
  • JavaScript - String Format
  • JavaScript - Length Property
  • JavaScript - Prototype
  • JavaScript - Slice Substr Vs Substring
  • JavaScript - JavaScript:void(0)

Other Links

  • JavaScript - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW