JavaScript Tutorial/String/indexOf — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 08:24, 26 мая 2010
Get a substring index
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
<!--
function myFunction()
{
var email_add=window.prompt("What is your e-mail address?","");
if (email_add.indexOf("@") == -1)
{
window.alert("You need an "@" symbol in your address, please try again.");
}
if (email_add.indexOf(".") == -1)
{
window.alert("You need a "." symbol in your address, please try again.");
}
if ( (email_add.indexOf("@") != -1) && (email_add.indexOf(".") != -1) )
{
document.write("Thanks!");
}
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<INPUT type="button" value="Click to enter an e-mail address" onClick="myFunction();">
</BODY>
</HTML>
String.indexOf()
Syntax
string.indexOf(string, num)
string.indexOf(string)
Use indexOf method the search a substring
<html>
<head>
<title>Using the indexOf() method</title>
<script type="text/javascript" language="javascript">
<!-- //
var myString = "A ab abc abcd";
var mySubstring = "abc";
var foundAtPosition;
foundAtPosition = myString.indexOf(mySubstring,0);
document.write("<P>The string " + mySubstring + " was found at position " + foundAtPosition);
// -->
</script>
</head>
<body>
</body>
</html>