Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2002
    Location
    San Antonio, TX
    Posts
    57
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Simple Javascript Form Validation

    I'm trying to do a simple validation to see if the field is blank, if it is, I want to write a message to a div. I've added the function call to an onclick. It seems to be set off no matter if there is a value or not, and what's more, the fields are clearing out when the link is clicked. My brain is mush right now, so what am I missing?

    Code:
    function validateThis() {
    	document.getElementById('messages').innerHTML = ''
    	var theMessage = '';
    	var reqMsg = 'must have a value.';
    	
    	for (var i = 0; i <document.theForm1.elements.length; i++) {
    	if (document.theForm1.elements[i].value = ' ') {
    		theMessage += document.theForm1.elements[i].name +' ' + reqMsg +'<br/>';
    	}}
    	
    	document.getElementById('messages').innerHTML = theMessage;
    }
    ----------------------------------------
    *Let's All Do Kamikaze Shots!*
    http://www.kamikazeshots.com

  • #2
    New Coder
    Join Date
    Sep 2002
    Location
    San Antonio, TX
    Posts
    57
    Thanks
    3
    Thanked 0 Times in 0 Posts
    disregard. I figured it out.

    Code:
    if (document.theForm1.elements[i].value = ' ') {
    should be

    Code:
    if (document.theForm1.elements[i].value == ' ') {
    Told you my brain was mush!
    ----------------------------------------
    *Let's All Do Kamikaze Shots!*
    http://www.kamikazeshots.com


  •  

    Posting Permissions

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