You are here:   HomeJava ScriptJavascript StringJavaScript IndexOf
by Dinesh Thakur Category: Javascript String

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.

 





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP 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. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Search Content







Advance Courses



Basic Courses



Advertise with Us