Darren Cook
Darren Cook

Reputation: 721

Javascript indexOf

I'm not well clued up with javascript so I'm having a problem getting the following script to work. I need to check if a name entered is also contained within a message.

<input type="hidden" id="Message" value="<%= rsDetail.Fields("Message") %>">
<input type="hidden" id="FirstName" value="<%= rsDetail.Fields("FirstName")%>">

<script type="text/javascript">
<!--
function NameCheck(){
var FirstName=document.getElementByID('FirstName');
var CardMessage=document.getElementByID('Message');
var aPosition = CardMessage.indexOf('FirstName');

if (aPosition == -1)
alert("Name Not In Message.");
}
-->
</script>

<a href="NextPage.asp" onClick="NameCheck();">Proceed</a>

Upvotes: 6

Views: 11287

Answers (7)

Supriya
Supriya

Reputation: 581

The indexOf() method returns the position of the first occurrence of a specified value in a string. This method returns -1 if the value to search for never occurs. Note: The indexOf() method is case sensitive.

Upvotes: 0

Mindaugas
Mindaugas

Reputation: 326

Best way to use jQuery. Your code we can minified to max 2 line:

$("#click").click(function() {
    var found = $('#Message').val().indexOf($("#FirstName").val());
    console.log(found);
});

Upvotes: 1

taskinoor
taskinoor

Reputation: 46037

'FirstName' with quote is string, not the variable FirstName. You need:

// remove the quote, pass the variable FirstName instead of string 'FirstName'
var aPosition = CardMessage.indexOf(FirstName);

EDIT: I missed two things before. First you need to get the value of the node, and second is the uppercase D. So the correct code is:

var FirstName = document.getElementById('FirstName').value;
var aPosition = CardMessage.indexOf(FirstName);

Upvotes: 1

Michael Berkowski
Michael Berkowski

Reputation: 270677

It seems like you are trying to get the value of the input FirstName. getElementById() only returns the node itself. Instead access its value:

var FirstName = document.getElementById('FirstName').value;
var CardMessage = document.getElementById('Message').value;

// Then use the variable `FirstName` instead of the quoted string
var aPosition = CardMessage.indexOf(FirstName);

// Best practice would be to use === for strict type comarison here...
if (aPosition === -1)
  alert("Name Not In Message.");
}

Also, note that you've misspelled getElementById, with a capital D at the end where it should be lowercase.

Upvotes: 12

campino2k
campino2k

Reputation: 1671

This what you are trying, I think.

var FirstName=document.getElementById('FirstName').value;
var CardMessage=document.getElementById('Message').value;
var aPosition = CardMessage.indexOf( FirstName );

Upvotes: 1

David Laberge
David Laberge

Reputation: 16061

Try :

var FirstName=document.getElementByID('FirstName');
var aPosition = CardMessage.indexOf(FirstName);

In your exemple you are looking for the following string FirstName not the value of the variable FirstName

Upvotes: -1

Aziz Shaikh
Aziz Shaikh

Reputation: 16524

document.getElementByID should be document.getElementById

Upvotes: 0

Related Questions