www.webdeveloper.com
Results 1 to 3 of 3

Thread: validating input

  1. #1
    Join Date
    Dec 2013
    Posts
    10

    validating input

    I am taking a JavaScript course and the instructor gave this as an example the function is called from the .js file:

    <input type="text" id="myText" >
    <input type="button" onclick="notEmpty()" value="Form Checker" >

    function notEmpty() {
    var myTextField = document.getElementById('myText');
    if(myTextField.value != "")
    alert("You entered: " + myTextField.value)
    else
    alert("Would you please enter some text?") }

    I am trying to convert it to accept input

    <input type="text" id="myText" >
    <input type="button" onclick="notEmpty2("myText")" value="Form Checker" >
    (also tried this line w/o the " ") -> <input type="button" onclick="notEmpty2(myText)" value="Form Checker" >

    function notEmpty2(chkThis) {
    var myTextField = document.getElementById(chkThis);
    if(myTextField.value != "")
    alert("You entered: " + myTextField.value)
    else
    alert("Would you please enter some text?") }

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    You're using the same quotes within your <input>'s event, try single quotes inside instead:

    Code:
    <input type="button" onclick="notEmpty2('myText');" value="Form Checker" />
    JavaScript: Learn | Validate | Compact

  3. #3
    Join Date
    Dec 2013
    Posts
    10
    Oh I didn't even think to try that. It worked.
    Thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles