Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Detect IE browser | Javascript

While working with Web projects those required cross browser compatibility we often need to detect the browser type,like ie, Firefox Mozilla, or chrome...etc.

In this post we will see how to find ie browser. This is done using navigator object in JavaScript. Navigator has appName property that we can use to find ie browser.

JavaScript to detect ie

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<script type="text/javascript">
    function getBrowserName()
    {
        if(navigator.appName=="Microsoft Internet Explorer") {
            alert("This is " + navigator.appName + " Browser");
        }
  }
</script>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="button" onclick="return getBrowserName();" value="Get Browser Name"/>
    </div>
    </form>
</body>
</html>

//Above code will show alert message "This is Microsoft Internet Explorer Browser"

Here we will see how to detect IE browser using jQuery

Regex for email validation JavaScript

In this post we will see how to validate email format in Javascript using regular expression. We can use regular expression (/^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$/i) to validate the email address in javascript.

Regular expression to validate email format in JavaScript

Example of email validation using regex in javascript

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Regex to validate email format in JavaScript</title>
  <script language="javascript" type="text/javascript">
    function Validate() {
      var emailRegex = new RegExp(/^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$/i);
      var emailAddress = document.getElementById("<%= txtemail.ClientID %>").value;
      var valid = emailRegex.test(emailAddress);
      if (!valid) {
        alert("Invalid e-mail address");
        return false;
      } else
        return true;
    }
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:TextBox runat="server" ID="txtemail"></asp:TextBox>
    <asp:Button runat="server" ID="Send" OnClientClick="return Validate();" Text="Send" />
  </div>
  </form>
</body>
</html>

Confirmationbox in javascript

show confirmationbox in javascript
In asp.net we need to show javascript confirmation box before doing some action.For example if we want to delete some record on the form, and we need users confirmation before deletion happens.

Sample code to show confirmation box in Javascript

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function Confirmation() {
if (confirm("Do you want to delete record?") == true)
return true;
else
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="Submit" onclick="return Confirmation();" />
</div>
</form>
</body>
</html>

Get IndexOf string in Javascript

string indexof in javascript
In JavaScript strings IndexOf() function is used to find position of specified character in the string. When IndexOf() method is used in javascript it returns position of first occurrence of the given character. If specified character is not found in string then it returns –1 value.

Syntax for IndexOf()


string.indexOf(searchstring, start)

Here search string is the character that is to be find and start is the position from which search to begin.

Example of IndexOf() Method [JavaScript]:


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function GetStringIndex() { 
var str="Javascript IndexOf Method.";
document.write( "a is at position"+ str.indexOf("a") + "<br />");
document.write("IndexOf is at position"+ str.indexOf("IndexOf") + "<br />");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="Submit" onclick="return GetStringIndex();" />
</div>
</form>
</body>
</html>

//Output<
//a is at position 1
//IndexOf is at position 11

Split string JavaScript

split string in javascript
In JavaScript we need to split the string to perform some operations on string. JavaScript uses Split() method to split the given string in JavaScript. Split() method basically split string into array of substrings and generates new array.

String split Syntax:


string.split(separator, limit)

Here separator is a character by which we want to separate string. and limit is to give up to how many number split the string.

Sample to split string [JavaScript]


<script type="text/javascript">
 function splitString()
 {
  var str="Split Javascript String";
  document.write(str.split(" "));
 }
</script> 


//Output
//Split, Javascript, String

In above example of string split, we give space(“ ”) character to split the string with. so it splits the string with “ “ and returns array with substrings.

do Postback in Javascript [ASP.NET]

dopostback javascript
In this post we will see how to postback page in ASP.NET from the client side using JavaScript. In some cases we need to postback page from client side i.e. by using JavaScript.
We can use __doPostBack() to postback page from client script using JavaScript.Or we can use Page.GetPostBackEventReference() to do postback from JavaScript. When we use Page.GetPostBackEventReference() it internally get converted to __doPostBack().

Sample that shows how to do postback from JavaScript [ASP.NET, C#]:


< input type="button" id="BaseButton1" onclick="submitPage()" value="OK"/>

Now after clicking on button it will call JavaScript function that will do the postback and then in page load we will capture and perform the operation that is needed.In code behind we use Request[ "__EVENTARGUMENT"] to get the request so that we can perform some operation on that event argument.So below is JavaScript function that uses Page.GetPostBackEventReference() and do the postback from Javascript.


function submitpage() 
{ 
  // trigger postback from JavaScript
   <%= this.Page.GetPostBackEventReference(this,"Delete")%> 
} 


GetPostBackEventReference Here “this” parameter in Page.GetPostBackEventReference(this, “Delete”) is the controls id and referred to as __EVENTTARGET and accessed using Request[“__EVENTTARGET”] in code behind to determine which control caused postback from JavaScript.And “Delete” is __EVENTARGUMENT and accessed using Request[ "__EVENTARGUMENT"] in code behind.

So now in code behind we can check for Request[ "__EVENTARGUMENT"] and perform some operations based on that condition like:


protected void Page_Load(object sender, EventArgs e) 
{ 
string eventArg = Request["__EVENTARGUMENT"]; 
if (!String.IsNullOrEmpty(eventArg)) 
{ 
 if(eventArg == "Delete")
 DeleteRecords();
}
} 

private void DeleteRecords() 
{ 
 //Do something
}