Implementing Text Box Control Validation within a Gridview using UpdatePanel in c# ASP.Net

In my gridview, one of the columns contains a Text Box Control.

I am looking to validate the text entered by users as alphanumeric characters and spaces only.

Allowed characters are: a-z, A-Z, 0-9, and space.

I want to perform this validation using JavaScript.

Platform: ASP.Net 2.0, C#

Here is what I have attempted so far...

<script type="text/javascript">

function IsValidCharNum(event) {
    var KeyBoardCode = (event.which) ? event.which : event.keyCode;
    if ((KeyBoardCode < 96 || KeyBoardCode > 123) && (KeyBoardCode < 65 || KeyBoardCode > 90) && (KeyBoardCode < 48 || KeyBoardCode > 57) && (KeyBoardCode !== 32)) {
        return false;
    }
    return true;
} </script>

When used with onkeypress="return IsValidCharNum(event)" for a textbox outside of the gridview and update panel, it works as expected.

Answer №1

If you want to ensure that a user only enters alphanumeric characters, you can use the RegularExpression Validator in the following way:

<asp:TextBox ID="txtName" runat="server" ></asp:TextBox>

<asp:RegularExpressionValidator ID="REValphaOnly" runat="server" ErrorMessage="Please enter only alphanumeric." ControlToValidate="txtName" ValidationExpression="^[a-zA-Z0-9 ]+$"></asp:RegularExpressionValidator>

For more information, you can visit these links:

http://msdn.microsoft.com/en-us/library/ms972966.aspx

http://www.codeproject.com/Tips/472728/RegularExpressionValidator-In-ASP-NET

Answer №2

To implement this functionality, you can utilize JavaScript to trigger an event when a user types in a textbox.

<script type="text/javascript>
        function ValidateText(i) {
            if (/[^0-9a-bA-B\s]/gi.test(fieldname.value)) {
                alert("Please enter only alphanumeric characters and spaces in this field");
                fieldname.value = "";
                fieldname.focus();
                return false;
            }
        }
</script>

<asp:TemplateField>
    <ItemTemplate>
        <asp:TextBox ID="TextBox1" runat="server" onkeyup ="ValidateText(this);"></asp:TextBox>
    </ItemTemplate>
</asp:TemplateField>

Answer №3

After much effort, I have successfully developed a function to validate user input

page.aspx

<script type="text/javascript>
  function CheckInput(event) {

      var KeyCode = (event.which) ? event.which : event.keyCode;

      if ((KeyCode < 96 || KeyCode > 123) && (KeyCode < 65 || KeyCode > 90) && (KeyCode < 48 || KeyCode > 57) && (KeyCode < 32 || KeyCode > 32)) {
         
          return false;

      }

      return true;

  }

<asp:TextBox ID="TextBox1" runat="server" onkeypress="return CheckInput(event)"></asp:TextBox>

Implementing this function with gridview and updatepanel functionality

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Instructions for properly formatting a date extracted from ASPX JSON through AJAX

Recently, I have been extracting data from a MySQL database, converting it into JSON format, and using AJAX to display the data on a Chart.JS. However, there is an issue with the date values being formatted as: /Date(154564434687)/ Below is the JQuer ...

Integrating AngularJS code into dynamically generated HTML using JavaScript

I am currently utilizing an outdated version of AngularJS (1.3). I have a page where I need to dynamically display different content based on database values. If the user interacts with the page and changes the database value, I want the displayed content ...

How can I access the parent function within module.exports?

Hello, I am having issues with this code because I am unable to access checkIf in order to set its property LengthIs. When I log whether this is equal to module.exports, it returns false. Upon further inspection, I also checked what this was and it retur ...

Unable to assign an argument to a function in commander.js

Having some trouble passing an option to a custom command in commander.js... program .command('init [options]') .description('scaffold the project') .option('-b, --build', 'add "build" folder with subfolders') . ...

What is the best way to remove every other second and third element from an array?

I am looking to remove every second and third element of an array in Javascript. The array I am working with is as follows: var fruits = ["Banana", "yellow", "23", "Orange", "orange", "12", "Apple", "green", "10"]; My goal is to delete every second and ...

The Material UI Icon rendered using document.createElementNS in React is failing to load properly

I'm currently developing a tags section for my app, similar to the tags feature in a new Stack Overflow question form. For this, I am utilizing the Material UI close icon. You can see how it is implemented in this tutorial on YouTube. (I have confirme ...

Can JavaScript event listeners be compelled to trigger in a specific sequence?

Is there a way in JavaScript to receive notification or execute a callback function once an event has completed its propagation? Put differently, is it possible to 'prioritize' an event and ensure that it gets triggered after all other event lis ...

Unable to retrieve element using getElementById with dynamically assigned id

After researching on Google and browsing through various Stack Overflow questions (such as this & this), I have not been able to find a solution. I am currently working on validating dynamically generated rows in a table. The loop and alert functions a ...

Tips for accessing private variables

After running this code in nodejs, I noticed that the 'Count' becomes negative for large values of 'count'. It made me wonder, is it the fault of 'count' or 'chain'? What would be the best approach to modify the &apo ...

Implement Ajax functionality in C# MVC to dynamically update the values of two dropdown filters

I have created a custom MVC web application that features a table with data that can be filtered using two dropdown menus. One dropdown allows me to select the state value of the data, while the other lets me choose if the data is closed or not (yes/no). W ...

Enhance global variable by appending a line from a local function in Javascript

In my js files, I have some global functions that are used in all modules of the application. Currently, I am working on a module that requires modifying one of these global functions to run a local script every time it is called. The issue is that the g ...

Leveraging AJAX for connectivity to the Twitter API

Considering incorporating Twitter functionality into my web application, I decided to conduct some tests. After researching how to call the search Twitter URL (more information available at: ) in order to retrieve tweets containing specific words or phrase ...

Is it possible to export CSS stylesheets for shadow DOM using @vanilla-extract/css?

I have been exploring the use of @vanilla-extract/css for styling in my React app. The style method in this library exports a className from the *.css.ts file, but I need inline styling to achieve Shadow DOM encapsulation. During my research, I came acros ...

Can you provide a guide on setting up and utilizing mathlive within NuxtJS?

Can anyone assist me? I am trying to figure out why my code is not working or if I have implemented it incorrectly. I used npm i mathlive to obtain input. However, following the instructions for implementing nuxt plugins in the documentation has not yield ...

Leveraging symbols as object key type in TypeScript

I am attempting to create an object with a symbol as the key type, following MDN's guidance: A symbol value may be used as an identifier for object properties [...] However, when trying to use it as the key property type: type obj = { [key: s ...

Can an infowindow be automatically closed based on specific criteria?

Show the infowindow only when hovering over the marker. It should disappear when moving the mouse away from the marker. The infowindow should stay open only if you click on the marker, and can be closed by clicking the close button on the infowindow. ...

How to Implement Button Disable Feature in jQuery When No Checkboxes Are Selected

I need help troubleshooting an issue with a disabled button when selecting checkboxes. The multicheck functionality works fine, but if I select all items and then deselect one of them, the button disables again. Can someone assist me with this problem? Be ...

React Intersection Observer Triggering Memory Leaks

Issue Description: I encountered a problem with my React app crashing on mobile. The Chrome dev tools revealed that garbage collection wasn't triggering, and upon inspecting the heap, I found that the top constructors by retained size were all linked ...

Tips for managing onClick code when a user selects "open link in new tab" within a React js environment

How can I ensure that my tracking code runs when a user clicks a button in my React project, even if they open it in a new tab? Is there a solution for this in React JS? Here's a simple example: var Hello = React.createClass({ render: function( ...

Ways to retrieve property value from a component in Vue.js 2.0

I am currently working with a specific component that allows for date selection: <template> <div class="animated fadeIn"> <div class="col-sm-6 col-lg-6"> <datepicker class="form-control" :value="config.state.fromDate" ...