Splitting Email ID into Different Parts

Lets take the help of the below code:
  1. <html>  
  2. <head>  
  3.     <title>JavaScript Tutorial using focus(), substring(), indexOf() methods</title>  
  4.     <script type="text/javascript">  
  5.         function getMainAndDomainParts() {  
  6.             var fullEmailId = document.getElementById("txtFullEmailId").value;  
  7.             if (fullEmailId == "") {  
  8.                 alert("Please Enter a Valid Email ID having \"@\" symobol");  
  9.                 document.getElementById("txtFullEmailId").focus();  
  10.             }  
  11.             else {  
  12.                 document.getElementById("txtMainPart").disabled = true;  
  13.                 document.getElementById("txtDomainPart").disabled = true;  
  14.   
  15.                 var mainPart = fullEmailId.substring(0, fullEmailId.indexOf("@"));  
  16.                 var domainPart = fullEmailId.substring(fullEmailId.indexOf("@") + 1);  
  17.   
  18.                 document.getElementById("txtMainPart").value = mainPart;  
  19.                 document.getElementById("txtDomainPart").value = domainPart;  
  20.             }  
  21.         }  
  22.     </script>  
  23. </head>  
  24. <body>  
  25.     <form id="form1">  
  26.         <div>  
  27.             <table style="border: 2px solid blue; font-family: 'Comic Sans MS'">  
  28.                 <h3 style="text-align: center">Email ID Parts Splitter</h3>  
  29.                 <tr>  
  30.                     <td>Enter full EmailID : </td>  
  31.                     <td>  
  32.                         <input id="txtFullEmailId" type="text" />  
  33.                     </td>  
  34.                     <td>  
  35.                         <input id="btnFetch" type="button" value="Fetch" onclick="getMainAndDomainParts();"  
  36.                             style="font-family: 'Comic Sans MS'" />  
  37.                     </td>  
  38.                 </tr>  
  39.                 <tr>  
  40.                     <td>Main Part : </td>  
  41.                     <td>  
  42.                         <input id="txtMainPart" type="text" />  
  43.                     </td>  
  44.                 </tr>  
  45.                 <tr>  
  46.                     <td>Domain Part : </td>  
  47.                     <td>  
  48.                         <input id="txtDomainPart" type="text" />  
  49.                     </td>  
  50.                 </tr>  
  51.             </table>  
  52.         </div>  
  53.     </form>  
  54. </body>  
  55. </html>  
In the above example, We want the user to enter a valid email id in the textbox and when the button is clicked, it splits the main part of email id and the domain part into different textboxes. To achieve this, We have included a JavaScript and some HTML elements that works with that script. Now, let's have a deep look at them.
  1. We have take 3 input textbox. One is for User input and other two are used to display the answer.
  2. A button is used to implement the javascript code.
JavaScript Explanation:
  1. We have created a user defined function 'getMainAndDomainParts()'. A variable 'fullEmailId' is declared which fetches and saves the data from the textbox 'txtFullEmailId'.
  2. Now, first we want to check that whether the input box is empty.
  3. If it is empty, so it should show an alert. We have used 'focus()' method to transfer the focus control to textbox 'txtFullEmailId' which helps the user to identify that which textbox is required to input email id.
  4. If it is not empty, 'txtMainPart' and 'txtDomainPart' textboxes are disabled to make them uneditable.
  5. The substring method takes two parameters i.e. 'start' and 'end'. The start specifies the value of a string from the index location 0 and end specifies the value upto which the string should be fetched. The end parameter does not include index location specifies in it. If end parameter is not specified, then the string is fetched till last.
  6. So, in this, we have fetched the value from the 0 location till the index value of "@" symbol and saves that value in the variable 'mainPart'.
  7. Now, we have to fetch the value after the "@" symbol which is the domain part. So, we have used substring method with its starting value as index value of "@" symbol and added 1 to it, so that it will not show "@" symbol in the answer and saves its value in the variable 'domainPart'.
  8. Now, we have assigned the mainPart and domainPart variables values to the textboxes txtMainPart and txtDomainPart respectively.
  9. At last, the function is called on the 'onclick' event of the button control.
HTML used is very simple and straight forward. So, I think its explanation is not required.

Next Recommended Reading How To Use Split Function In JavaScript