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>
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>
Share
Improve this question
asked Oct 19, 2011 at 13:11
Darren CookDarren Cook
7214 gold badges14 silver badges28 bronze badges
1
|
7 Answers
Reset to default 12It 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.
This what you are trying, I think.
var FirstName=document.getElementById('FirstName').value;
var CardMessage=document.getElementById('Message').value;
var aPosition = CardMessage.indexOf( FirstName );
'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);
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);
});
document.getElementByID
should be document.getElementById
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.
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
document.getElementById()
returns the element, not the element's value. To get the element's value, you can dodocument.getElementById('someId').value
. – aroth Commented Oct 19, 2011 at 13:14