We help IT Professionals succeed at work.

Compare first three characters of text box string value

MoJeaux39
MoJeaux39 asked
on
Medium Priority
2,791 Views
Last Modified: 2012-05-06
Hi -
I'm trying to compare the first three digits of a textbox value to a fixed string.  I've tried to identify the first three characters by using the CharAt() function, but keep getting "object doesnt support this property or method' error.  Any suggestions?   Pnvalu value should be a string that looks like 'ABC123456'.   I need to compare the first three characters to make sure the user entered in the correct invoice number.

Code is part of a larger validation script.  Thanks for your help!  
function checkRadio(main)  {
var pnvalu = document.all.INVOICE.value;
var size = pnvalu.length;
var comp = "ABC"
var size3 = pnvalu.CharAt(size-3);
pnvalu = pnvalu.toUpperCase();
alert(size3);
 
}

Open in new window

Comment
Watch Question

Commented:
var size3 = pnvalu.tostring.substring(0,3)
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008
Commented:
var size3 = pnvalu.substr(0,3);

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008

Commented:
>>but keep getting "object doesnt support this property or method' error.
The method is "charAt()" - lowercase "c" for "char", but it will only return ONE character, not a substring.
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008

Commented:
>>var pnvalu = document.all.INVOICE.value;
document.all will work only for IE. I suggest you give your input field an id:

<input id="Invoice" name="INVOICE" .../>
 
and use:
var pnvalu = document.getElementById("Invoice").value;

Open in new window

Author

Commented:
Thanks for the Assist!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.