Reece O'Sullivan Reece O'Sullivan - 1 year ago 98
Javascript Question

JavaScript taking information from a email address

I have been assigned a task at my university to create a web page with two text boxes and a button.

The user is to enter an email address in the format below into the first text box. The user then clicks the button and the surname will appear in the second text box. The email addresses is in the general form

My function must work for all of the following:


I have been told to use
to find the

<!DOCTYPE html>
<title> JavaScript </title>
<meta name="author" content="Reece">
<meta name="description" content="describe the website">
<link rel="stylesheet" href="Style.css" type="text/css">
<input type="text" id="myText" value=""> </br> </br> <button onclick="myFunction()">Submit</button>
<p id="demo"></p>
<script> function myFunction() { var x = document.getElementById("myText").value; document.getElementById("demo").innerHTML = x; } </script>

Answer Source

First get the email address to parse. You've already done that. Here, x contains the email address.

var x = document.getElementById("myText").value;

Now you can use x.indexOf(".") to get the position of the first period. Likewise, use x.indexOf("@") to get the position of the "@" symbol.

These two values are passed to x.substring() to get the text between the two.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download