Built-in functions alert function Contd

text zoom

Lesson 6 continued:



Next we add our JavaScript function to validate the form:


function check() {

var name = document.getElementById('name');

if(name.value != "")

alert("You entered: " + name.value)

else

alert("Would you please enter some text?")

}


now let's put it all together:


<html>

<head>

<title>Code Template</title>

<script type="text/javascript">

<!--

function check() {

var name = document.getElementById('name');

if(name.value != "")

alert("You entered: " + name.value)

else

alert("Would you please enter some text?")

}

//
-->

</script>

</head>

<body>

<form method="post" action="JS.js">

Name:<br>

<input type="text" id="name" name="name"> <br>

<input type="button" value="submit" onClick="check()">

</form>

</body>

</html>


Notice we put our function in the head section and we called it inside our form.If you copy this code into your notepad then run it, it will give you a textbox to enter your name and if you do not enter anything it will display an alert box with: "Would you please enter some text?" and if you do enter something, it will display an alert box that says what you entered! Pretty cool huh? That is only a very small taste of what you can do with JavaScript!

                    

Copyright © 2012 VisualBuilder. All rights reserved