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 5 of 5

Thread: Help with forms

  1. #1
    New to the CF scene
    Join Date
    Sep 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with forms

    Here is my problem,
    I have the user enter their email and then click on a button to validate it. Then using the length property of the string, I need to determine if the user has entered a value. And if they haven't, alert them that they need to. Then using the indexOfString method, I need to determine if the @ symbol is in their value, and if there is tell them that their email is valid, or if not, tell them it is invalid.
    Here is the script i have so far. Basically I am confused by the length propery and indexofString, I can't seem to get them to work properly. Thanks.

    <html>
    <script language="Javascript">

    function checkForm(){
    var usersEmail=document.address.myEmail.value;

    if (userEmail=""){


    alert("Please fill out Email");
    }
    else{
    alert(usersEmail);
    document.address.myEmail.value="";
    }
    }
    </script>


    <form name="address">
    <table border="1" align="center">
    <tr>
    <td>Email:
    </td>
    <td>
    <input type="text" name="myEmail" size="20"></td>
    </tr>

    <tr>
    <td colspan="2" align="center">
    <input type="button" name="runCode" value="Validate" onClick="checkForm()"></td></tr>
    <table>
    </table>
    </form>
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    	<head>
    		<title>47166</title>
    	</head>
    	<body>
    		<form onsubmit="return validate()">
    			<fieldset>
    				<label for="email">Email</label>
    				<input id="email" name="email" type="text" size="20">
    				<input type="submit">
    			</fieldset>
    		</form>
    	</body>
    	<script type="text/javascript">
    		function validate()
    		{
    			var email = document.getElementById("email");
    			if((email.value == '') || (email.value.search(/@/) < 0))
    			{
    				throw_error("Please fill out email","email");
    				return false;
    			}
    			else
    			{
    				alert(email.value);
    				email.value = '';
    			}
    			return true;
    		}
    
    		function throw_error(msg, obj)
    		{
    			alert(msg);
    			document.getElementById(obj).focus();
    		}
    	</script>
    </html>

  • #3
    New to the CF scene
    Join Date
    Sep 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks, but wouldn't I need to use string.length and string.IndexOf ? This is what I was told to use, but maybe I overlooked it.

  • #4
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Or you can do this:
    Code:
    <html>
    <script language="Javascript">
    
    function checkForm(){
    var usersEmail = document.address.myEmail.value;
    	if (usersEmail == "" || usersEmail.indexOf("@") == -1){
    		alert("Please fill out Email");
    	}
    
    	else{
    		alert(usersEmail);
    		document.address.myEmail.value="";
    
    	}
    }
    </script>
    <form name="address">
    <table border="1" align="center">
    <tr>
    <td>Email:
    </td>
    <td>
    <input type="text" name="myEmail" size="20"></td>
    </tr>
    <tr>
    <td colspan="2" align="center">
    <input type="button" name="runCode" value="Validate" onClick="checkForm()"></td></tr>
    <table>
    </table>
    </form>
    </body>
    </html>

  • #5
    New to the CF scene
    Join Date
    Sep 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ah yes, that works real good.
    much thanks!


  •  

    Posting Permissions

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