Trim spaces and validating a text box in a page without master page using javascript
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>JS Trim Without Master Page</title>
<script type="text/javascript">
function trim(textBoxValue) {
return textBoxValue.replace(/^\s+|\s+$/g, '');
}
function ValidateData() {
var txtName = document.getElementById("txtName");
txtName.value = trim(txtName.value);
if (txtName.value == "") {
alert("Please enter text in Name");
txtName.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Name: <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return ValidateData();" />
</div>
</form>
</body>
</html>
<head runat="server">
<title>JS Trim Without Master Page</title>
<script type="text/javascript">
function trim(textBoxValue) {
return textBoxValue.replace(/^\s+|\s+$/g, '');
}
function ValidateData() {
var txtName = document.getElementById("txtName");
txtName.value = trim(txtName.value);
if (txtName.value == "") {
alert("Please enter text in Name");
txtName.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Name: <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return ValidateData();" />
</div>
</form>
</body>
</html>
Trim spaces and validating a text box in a page with master page using javascript
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<script type="text/javascript">
function trim(textBoxValue) {
return textBoxValue.replace(/^\s+|\s+$/g, '');
}
function ValidateData() {
// when we use a masterpage, to get the id we should use "<%= txtName.ClientID %>" instead of "txtName"
var txtName = document.getElementById("<%= txtName.ClientID %>");
txtName.value = trim(txtName.value);
if (txtName.value == "") {
alert("Please enter text in Name");
txtName.focus();
return false;
}
return true;
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
Name:
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return ValidateData();" />
</asp:Content>
<script type="text/javascript">
function trim(textBoxValue) {
return textBoxValue.replace(/^\s+|\s+$/g, '');
}
function ValidateData() {
// when we use a masterpage, to get the id we should use "<%= txtName.ClientID %>" instead of "txtName"
var txtName = document.getElementById("<%= txtName.ClientID %>");
txtName.value = trim(txtName.value);
if (txtName.value == "") {
alert("Please enter text in Name");
txtName.focus();
return false;
}
return true;
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
Name:
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return ValidateData();" />
</asp:Content>