I am encountering issues with JavaScript validation in ASP.NET. The validation seems simple, but for some reason, it is not functioning properly.
Below is the code that I have tried:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="gpavalidation._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script>
function gpa()
{
var reg = /[0-9][.][0-9]/;
if(!reg.test(document.getElementById("cgpa").value))
{
return false;
}
else
{
return true;
alert("correct value");
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
GPA:<input type="text" name="cgpa" id="cgpa" maxlength="3" onclick="return gpa();" />
<input type="submit" name="name" value="submit " />
</div>
</form>
</body>
</html>
However, the code is not producing the desired outcome...
What I am looking to achieve: I want the user to only be able to input numeric data in the GPA field, such as 3.4, 4.0, or 2.1. How can I achieve this result?
Any assistance on this matter would be greatly appreciated.