Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Dynamic Text Using JavaScript?

Posted on 2009-04-08
3
Medium Priority
?
348 Views
Last Modified: 2012-05-06
I am trying to write a portion of my webpage so that when the user enters in a year into a textbox, next to it, the test changes to show the next year.  Ultimately it should liike like a year range, specifically for a school year (ie: 2000 - 2001, 2008 - 2009).  Additionally, I would like to be able to have the "dynamic year" say "[Invalid]" if the user had not typed in a complete year yet.. (ie: 200 - [Invalid]).  Once the user types in 4 digits, then the year range should display properly.

I know VB and and currently writing ASP pages...unfortunately, I am not very familer with JavaScript.  Below is the code I have, but I have no clue how to write the JavaScript part...can anyone help?
<span id="ctl00_MainContent_lblSchoolYear" style="display:inline-block;width:88px;">School-Year Start:</span>
<input name="ctl00$MainContent$txtSchoolYear" type="text" value="0000" maxlength="4" id="ctl00_MainContent_txtSchoolYear" style="width:32px;" />
<span id="ctl00_MainContent_lblSchoolYearRange"> - [EndYear]</span>

Open in new window

0
Comment
Question by:Ben Santiardo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 41

Accepted Solution

by:
HonorGod earned 2000 total points
ID: 24097258
Something like this perhaps?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> Years </title>
<script type='text/javascript'>
  function field( id ) {
    return document.getElementById( id )
  }
 
  function checkYear( obj, id ) {
    var target = field( id )
    if ( target ) {
      if ( target.nodeName == 'SPAN' ) {
        target = target.firstChild
      }
      if ( obj ) {
        if ( obj.nodeName == 'INPUT' ) {
          var val = obj.value
          var msg = '- [Invalid]'
          if ( /^\d+$/.test( val ) ) {
            if ( val == '0000' ) {
              msg = '- [EndYear]'
            } else {
              if ( val.length == 4 ) {
                msg = '- ' + ( ( val - 0 ) + 1 )
              }
            }
          } else {
            alert( 'checkYear() - non-numeric value entered.' )
          }
          target.nodeValue = msg
        } else {
          alert( 'checkYear() - parameter error. "INPUT" expected, "' + obj.nodeName + '" provided' )
        }
      } else {
        alert( 'checkYear() - parameter error. Document element required.' )
      }
    }
  }
</script>
</head>
<body>
 
<span id="ctl00_MainContent_lblSchoolYear" style="display:inline-block;width:88px;">School-Year Start:</span>
<input name="ctl00$MainContent$txtSchoolYear" type="text" value="0000" maxlength="4" id="ctl00_MainContent_txtSchoolYear" style="width:32px;" onkeyup='checkYear(this,"ctl00_MainContent_lblSchoolYearRange")' />
<span id="ctl00_MainContent_lblSchoolYearRange"> - [EndYear]</span>
 
</body>
</html>

Open in new window

0
 

Author Comment

by:Ben Santiardo
ID: 24097563
PERFECT!!  That was exactly what I needed.
Thank you so much!
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24097700
You are very welcome.  Thanks for the grade & points.

Good luck & have a great day
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question