Title: Javascript check to see if string starts with ‘ste’ OR ‘Ste’

This page was last modified on June 23, 2008

Hello visitors
I thought to update this page as this page has had 9885 views so far as obvious in my statistics copied here
Top Posts Title: Javascript check to see if string starts with ’ste’ OR ‘Ste’, 11,191 views
so being the hottest item on my blog, it deserves more attention and work.

I know you came here looking for a way to do string processing in Javascript. For example you would like to search the text within a

<html>
<head>
<script language=javascript>

function CheckIt()
{
var element = document.getElementById(“theString”).value;
var subSection = element.substring(3,0);
var answer = false;

if(subSection == “ste” )
answer = true;

if(subSection == “Ste” )
answer = true;

alert(answer);

}

</script>
</head>
<body>

<input type=text id=theString><input type=button value=”Check” onclick=CheckIt()>

</body>
</html>

Also, if you want to compare ‘ste’ in any case, you could just:

function CheckIt()
{
var element = document.getElementById(“theString” ).value;

var subSection = element.substring(3,0).toLowerCase();
var answer = (subSection == “ste”);

alert(answer);
}

You can post your comments here and I will be answering your queries about JavaScript on this page. So post a comment and get yourself an answer to your Javascript problem

5 Responses to Title: Javascript check to see if string starts with ‘ste’ OR ‘Ste’

  1. vaticanism says:

    Nice blog. I like your design. How do you make a variable into a string tho?

  2. fahdi says:

    As this page is very popular among my readers, so it was updated again today 21st May 2008.

  3. fahdi says:

    vaticanism:

    You can make a variable into a string in a very easy way. So here is a very good resource that will help you converting any javascipt variable into a string

    http://www.jibbering.com/faq/faq_notes/type_convert.html#tcString


    Regards
    Fahd Murtaza

  4. ddsp says:

    Why not use a regular expression to check for the start and allow both upper and lower case “S”, i.e. /^[Ss]te/. The ^ shows the start of the string, the [Ss] shows either upper or lower case S for the first letter, and the te show the next two letters.

    The string.match() function will return the string or null.

    Also, you should probably be sure that the string exists to avoid run time errors that may result in unpredictable performance.

    function checkit()
    {
    var element=document.getElementById(”theString”);
    if (element != null)
    { return (element.value.match(/^[Ss]te/) != null); }
    else
    { return false; }
    }

  5. Pingback: Need help programming

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s