user3547196
user3547196

Reputation: 1

Creating a Custom Tag To use if a input field is empty

I want to create a custom TLD Tag that assigns an asterisk beside the input field if the field is empty after the user submits. What would be the best way to do this? I am thinking of using but can I do this all from the form, or should I go to the servlet and back? I guess what I am getting at is what is the best way to do this?

My Form

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="datetime" tagdir="/WEB-INF/tags" %>
  <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>   
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Assignment 4</title>
<style type="text/css">@import url(/css/style.css);</style>
</head>
<body>
<div id="registration">
<form action="registrationServlet" method="post">
<c:set var="customer" scope="session" value="${session.customer}" />
    First Name:<br>
        <input type="text" name="firstName" value="${customer.getFirstName() }"/><br>
    Last Name:<br>
        <input type="text" name="lastName" value="${customer.getLastName }"/><br>
    Email:<br>
        <input type="text" name="emailAddress" value="${customer.getEmailAddress }"/><br>
    Password:<br>
        <input type="password" name="password"/><br>
        <input type="submit" value="Submit"/><br>
        Current Date/Time: <datetime:CurrentDateTime/>
</form>
</div>
</body>
</html>

Upvotes: 0

Views: 339

Answers (1)

Louise Miller
Louise Miller

Reputation: 3199

Theres a few possiblities for implementing this. The simplest might be to just do it in javascript.

You can intercept the form being submitted, check if the fields are empty and if they are, append the asterisk. If not, let the form be submitted.

e.g add onsubmit to form

<form id="myform" onsubmit="return validateForm()">
    <input type="text" id="myinput">

...

Then using javascript and jquery :

function validateForm() {
    if ($('input:text').is(":empty")) {

        $('input:text').after("<span>*</span>");
        return false;
    }
    return true;
}

The more robust way would be to implement server side validation aswell. If you're using spring mvc, theres loads of resources of how to do that.

Upvotes: 1

Related Questions