Sunday, February 21, 2010

How to create javascript alerts


How to Create a JavaScript Alerts?




Following example shows you how to create a JavaScript Alerts.





JavaScript alert is a kind of message box that use to display some information to the user to gain his attention or prompt a input from the user. Suppose that you want to display an error message to the user. Using javascript alert you can do it easily. When you validate user inputs before submit to the server, you can use javascript alert to ask user to enter valid data. Also you can use a confirm dialog box to ask user to make sure that he entered correct data.


Demo- Create JavaScript Alerts.

JavaScript Alert Boxes
  JavaScript alert box show a message to the user.

You can display the message with multiple lines using '\n'
» How to create Simple JavaScript Alert Box
» How to break lines in a javascript alert message
» Display custom message in javascript alert
  Enter your Message
» Display screen size using an alert
» Display the URL of current web page
 
JavaScript Prompt Messages
  JavaScript prompt dialog box provide a text box to the user to enter

text input. Also you can define a default value. It returns user's input if

the user has entered a value. Otherwise it returns the default value.

But it returns 'null' if there is no any value present in the text box.
» Prompt user name
» Adding two numbers using prompt messages
 
JavaScript Confirm Messages
  JavaScript confirm dialog box returns true or false according to
user response. If user clicks on 'Yes' button it returns 'true'
and if user
clicks on 'Cancel' button or close the message window it returns 'false'
» Simple confirm message
» Use confirm dialog to go to another web page



Try It!

Bookmark and Share





1. Example code for create a alert box in javascript.

JavaScript Code

<script type="text/javascript" language="javascript">

<!--

function simpleAlert(){

window.alert("This is a simple javascript alert");

return false;

}

-->

</script>

HTML Code


<html>
<head><title>How to create a simple alert </title>
</head><body>
<table border="0"><tr>
<td>&nbsp;<input name="btnmessage" type="submit" id="btnmessage" onclick="javascript:simpleAlert();" value="Click Here " /></td>
<td>&nbsp;</td></tr>
</table>
</body>
</html>




2. Example code for create a multi line alert in JavaScript.

JavaScript Code

<script type="text/javascript" language="javascript">

<!--

function lineBreakAlert(){

window.alert("You can display multiple lines in your message\n1.This is the second line\n2.This is third line\nUse \'\\n\' to break lines in your message");

return false;

}

-->

</script>

HTML Code


<html>

<head><title>How to create a multiple line alert </title>

</head><body>

<table border="0"><tr>

<td>&nbsp;<input name="btnmessage" type="submit" id="btnmessage" onclick="javascript:lineBreakAlert();" value="Click Here " /></td>

<td>&nbsp;</td></tr>

</table>

</body>

</html>




3. Example code for create a prompt dialog box in JavaScript.

JavaScript Code

<script type="text/javascript" language="javascript">

<!--

function promptName(){

var name=window.prompt("Please enter your Name","");

if(name==null){

alert("Please enter your name");

return false;

}else{

if(name!=""){

alert("Your Name is "+name);

return false;

}else{

alert("Please enter your name");

return false;

}

}

}

-->

</script>

HTML Code


<html>

<head><title>How to create a multiple line alert </title>

</head><body>

<table border="0"><tr>

<td>&nbsp;<input name="btnmessage" type="submit" id="btnmessage" onclick="javascript:promptName();" value="Click Here " /></td>

<td>&nbsp;</td></tr>

</table>

</body>

</html>




4. Example code for create a confirm dialog box in JavaScript.

JavaScript Code

<script type="text/javascript" language="javascript">

<!--

function simpleConfirm(){

var response=confirm("Click \'Ok\' or \'Cancel\'");

alert("You clicked "+(response==true?"Ok":"Cancel")+" button");



}

-->

</script>

HTML Code


<html>

<head><title>How to create a confirm dialog box </title>

</head><body>

<table border="0"><tr>

<td>&nbsp;<input name="btnmessage" type="submit" id="btnmessage" onclick="javascript:simpleConfirm();" value="Click Here " /></td>

<td>&nbsp;</td></tr>

</table>

</body>

</html>





 »   How to get screen resolution in JavaScript
 »   How to limit characters in textarea using JavaScript
 »   How to validate decimal number in JavaScript
 »   How to validate an email address in JavaScript
 »   How to validate date using JavaScript
 »   JavaScript String functions
 »   How to validate multiple select list box in JavaScript
 »   How to generate random numbers in JavaScript
 »   How to validate multiple check box in JavaScript
 »   How to validate user login in JavaScript
 »   How to validate drop down list in JavaScript
 »   How to validate radio button group in JavaScript
 »   How to create JavaScript alerts
 »    How to create popup windows in JavaScript
 »   How to count words in a text area using JavaScript




©-Copyright By Duminda Chamara   
JavaScript Validation  

0 comments: