Sunday, 26 May 2013

Javascript to varify email address..



Javascript to varify email Address..



This script uses regular expressions to check that a form field contains a valid email address.

Add the following script in your page..


<!DOCTYPE html>
 
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript">
        var emailfilter = /^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
 
        function checkmail() {
            var emailIdToCheck = document.getElementById('myemail').value;
 
            if ((emailIdToCheck == null) || (emailIdToCheck == "")) {
                document.getElementById('result').style.color = "red";
                document.getElementById('result').innerHTML = "Please Enter your Email ID";
                document.getElementById('myemail').focus()
                return false
            }
 
            var returnval = emailfilter.test(emailIdToCheck);
            if (returnval == false) {
                document.getElementById('result').style.color = "red";
                document.getElementById('result').innerHTML = "Entered email id : "+ emailIdToCheck + " is not valid email id.";  
            }
            else {
                document.getElementById('result').style.color = "green";
                document.getElementById('result').innerHTML = "Entered email id : " + emailIdToCheck + " is valid email id.";
            }
            return false;
        }
    </script>
</head>
<body>
    <input name="myemail" id="myemail" type="text" style="width: 270px" />
    <input type="submit" onclick="javascript: return checkmail();" value="Submit" />
    <br/>
    <br/>
    <div id='result'></div>
 
</body>
</html>

The code above includes a sample form where this script validates the email field contained.

Output will be as following.


It will show required field message if email id field is blank.
Javascript to varify email Address



Following will be result if you enter valid Email Address in text box.
Javascript to varify email Address



Following will be result if you enter invalid Email Address in text box.
Javascript to varify email Address



Regular expression to validate email address belonging to some specific domains.

        var emailfilter = /^([\w-\.]+@(?!gmail.com)(?!yahoo.com)(?!hotmail.com)([\w-]+\.)+[\w-]{2,4})?$/;


Another way to check valid email address using javascript is to check following points only.

1. Check that email address has only one (@) and after the @ atleast one (.) and it is not at starting or ending position.
2. And there should not be any spaces, extra '@'s or a (.) just before or after the @..

<!DOCTYPE html>
 
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript">
        function checkmail() {
            var emailIdToCheck = document.getElementById('myemail').value;
 
            //To check if email id field is not blank.
            if ((emailIdToCheck == null) || (emailIdToCheck == "")) {
                document.getElementById('result').style.color = "red";
                document.getElementById('result').innerHTML = "Please Enter your Email ID";
                document.getElementById('myemail').focus();
                return false
            }
            var at = "@"
            var dot = "."
            var lat = emailIdToCheck.indexOf(at)
            var lstr = emailIdToCheck.length
            var ldot = emailIdToCheck.indexOf(dot)
            if (emailIdToCheck.indexOf(at) == -1) {
                document.getElementById('result').style.color = "red";
                document.getElementById('result').innerHTML = "Entered email id : " + emailIdToCheck + " is not valid email id.";
                return false;
            }
 
            if (emailIdToCheck.indexOf(at) == -1 || emailIdToCheck.indexOf(at) == 0 || emailIdToCheck.indexOf(at) == lstr) {
                document.getElementById('result').style.color = "red";
                document.getElementById('result').innerHTML = "Entered email id : " + emailIdToCheck + " is not valid email id.";
                return false
            }
 
            if (emailIdToCheck.indexOf(dot) == -1 || emailIdToCheck.indexOf(dot) == 0 || emailIdToCheck.indexOf(dot) == lstr) {
                document.getElementById('result').style.color = "red";
                document.getElementById('result').innerHTML = "Entered email id : " + emailIdToCheck + " is not valid email id.";
                return false;
            }
 
            if (emailIdToCheck.indexOf(at, (lat + 1)) != -1) {
                document.getElementById('result').style.color = "red";
                document.getElementById('result').innerHTML = "Entered email id : " + emailIdToCheck + " is not valid email id.";
                return false;
            }
 
            if (emailIdToCheck.substring(lat - 1, lat) == dot || emailIdToCheck.substring(lat + 1, lat + 2) == dot) {
                document.getElementById('result').style.color = "red";
                document.getElementById('result').innerHTML = "Entered email id : " + emailIdToCheck + " is not valid email id.";
                return false;
            }
 
            if (emailIdToCheck.indexOf(dot, (lat + 2)) == -1 || emailIdToCheck.indexOf(" ") != -1) {
                document.getElementById('result').style.color = "red";
                document.getElementById('result').innerHTML = "Entered email id : " + emailIdToCheck + " is not valid email id.";
                return false;
            }
            document.getElementById('result').style.color = "green";
            document.getElementById('result').innerHTML = "Entered email id : " + emailIdToCheck + " is valid email id.";
            return false;
        }
    </script>
</head>
<body>
    <input name="myemail" id="myemail" type="text" style="width: 270px" />
    < input type="submit" onclick="javascript: return checkmail();" value="Submit" />
    <br/>
    <br/>
    <div id='result'></div>
 
</body>
</html>

Output will be same as described above for first method.


Saturday, 20 April 2013

Get query string values using javascript.


Get query string values using javascript.


You can use the pure JavaScript to read query string paramemeter value,

function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.search);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

if following is an url.

http://www.test.com/test.html?name=bridzetech

then you can following javascript method to read name value in query string.

function readname()
{
      var nameval = getParameterByName('name');
alert(nameval);
}

that's it.

How to make a first letter capital in C#


How to make a first letter capital in C#


How can the first letter in a word of sentence using c#.
as an example

it is a text.  = It is a text.

solution is use below function.

public static string ToUpperFirstLetter(this string source)
{
    if (string.IsNullOrEmpty(source))
        return string.Empty;
    // convert to char array of the string
    char[] letters = source.ToCharArray();
    // upper case the first char
    letters[0] = char.ToUpper(letters[0]);
    // return the array made of the new char array
    return new string(letters);
}
another good solution is 

String after = before.Substring(0, 1).ToUpper() + before.Substring(1);

so you can use above solution.




Sunday, 14 April 2013

Sql server - Rows to table


Sql server - Rows to table...


Using pivot ans unpivot

You can use the PIVOT and UNPIVOT relational operators to manipulate a table-valued expression into another table. PIVOT rotates a table-valued expression by turning the unique values from one column in the expression into multiple columns in the output, and performs aggregations where necessary on any remaining column values that are desired in the final output. UNPIVOT performs the opposite operation to PIVOT by rotating columns of a table-valued expression into column values. Every PIVOT query involves an aggregation of some type.

There are two ways to pivot data:
  1.     We can convert multiple rows into a single row with repeating groups of columns.
  2.     We can convert columns on a single row into multiple rows with a generic column and a row type discriminator.

Use the following scripts,

CREATE TABLE Sales (State CHAR(2), SalesAmt DECIMAL(18,2))  

Insert values,

INSERT INTO Sales VALUES ('ND',10000)  
INSERT INTO Sales VALUES ('SD',30000)  
INSERT INTO Sales VALUES ('TN',2500.50)  
INSERT INTO Sales VALUES ('OR',5500.50)  
INSERT INTO Sales VALUES ('VA',6500.50)  
INSERT INTO Sales VALUES ('SD',7000)  
INSERT INTO Sales VALUES ('ND',8000)  
  SELECT * FROM Sales  

Here is the result set,


Run the following query,

SELECT [ND],[SD],[TN],[OR],[VA]  
FROM (SELECT State,SalesAmt FROM Sales) p  
PIVOT
(
SUM (SalesAmt)  
FOR State IN  
([ND],[SD],[TN],[OR],[VA])  
AS pvt  

Here is the result set,



UNPIVOT performs almost the reverse operation of PIVOT, by rotating columns into rows.
Use the following scripts,

Create the following table,  
CREATE TABLE StudentMarks(  
[NameVARCHAR(50),  
Subject1 VARCHAR(10),  
Mark1 INT,  
Subject2 VARCHAR(10),  
Mark2 INT,  
Subject3 VARCHAR(10),  
Mark3 INT)  

Insert some data into above table ans suppose select returns following result set,


Run the following query,

SELECT  [Name], SubjectName,  
case when Subject='Subject1' then Mark1  
     when Subject='Subject2' then Mark2  
     when Subject='Subject3' then Mark3  
    else NULL end as Marks  
FROM  
   (SELECT [Name], Subject1,Mark1, Subject2,Mark2, Subject3,Mark3  
   FROM StudentMarks) p  
UNPIVOT
   (SubjectName FOR Subject IN 
   (Subject1, Subject2, Subject3)
    )AS unpvt;  
Here is the result set,


Note that UNPIVOT is not the exact reverse of PIVOT. PIVOT performs an aggregation and hence merges possible multiple rows into a single row in the output. UNPIVOT does not reproduce the original table-valued expression result because rows have been merged. Besides, NULL values in the input of UNPIVOT disappear in the output, whereas there may have been original NULL values in the input before the PIVOT operation.