Slip No 7 Q A

Write a JSP script to validate given E-Mail ID. [15 M]

email.html

<!DOCTYPE html>
<html>
<head>
    <title>Email Validation</title>
</head>
<body>
    <h2>Enter Your Email ID</h2>
    <form action="validateEmail.jsp" method="post">
        Email: <input type="text" name="email" required />
        <br><br>
        <input type="submit" value="Validate">
    </form>
</body>
</html>
validateEmail.jsp (JSP Validation Script)
<%@ page import="java.util.regex.*" %>
<!DOCTYPE html>
<html>
<head>
    <title>Email Validation Result</title>
</head>
<body>

<%
    String email = request.getParameter("email");

    if(email != null) {

        String emailRegex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$";
        Pattern pattern = Pattern.compile(emailRegex);
        Matcher matcher = pattern.matcher(email);

        if(matcher.matches()) {
%>
            <h2 style="color:green;">Valid Email ID</h2>
<%
        } else {
%>
            <h2 style="color:red;">Invalid Email ID</h2>
<%
        }
    }
%>

</body>
</html>
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top