Invoke a Java script function for Regular Expression validation failure in ASP.NET

I have a

<asp:RegularExpressionValidator>
that validates a text box, and I also have a JavaScript function that prevents entering non-numerical values in the textbox. When I use the expression validator alone, it works fine. However, as soon as I add
onkeydown="return jsDecimals(event);"
to the text box to call the jsDecimals() function, the validator stops working. What could be causing this issue?

ASP Code

<asp:TextBox ID="TextBox2" runat="server" CssClass="form-control" CausesValidation="true" MaxLength="13" onkeydown="return jsDecimals(event);"></asp:TextBox> 

<asp:Button ID="Button5" runat="server" Text="Retrieve" CssClass="btn btn-default" OnClick="Button5_Click"/>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" CssClass="tooltip-arrow"
      ErrorMessage="ID must be 13 Numeric characters" ControlToValidate="TextBox2" ValidationExpression="^[0-9]{13}$">
</asp:RegularExpressionValidator>

JavaScript:

function jsDecimals(e) {

var evt = (e) ? e : window.event;
var key = (evt.keyCode) ? evt.keyCode : evt.which;
if (key != null) {
    key = parseInt(key, 10);
    if ((key < 48 || key > 57) && (key < 96 || key > 105)) {
        if (!jsIsUserFriendlyChar(key, "Decimals")) {
            return false;
        }
    }
    else {
        if (evt.shiftKey) {
            return false;
        }
    }
}
return true;
function jsIsUserFriendlyChar(val, step) {
// Backspace, Tab, Enter, Insert, and Delete  
if (val == 8 || val == 9 || val == 13 || val == 45 || val == 46) {
    return true;
}
// Ctrl, Alt, CapsLock, Home, End, and Arrows  
if ((val > 16 && val < 21) || (val > 34 && val < 41)) {
    return true;
}
if (step == "Decimals") {
    if (val == 190 || val == 110) {  //Check dot key code should be allowed
        return true;
    }
}
// The rest  
return false;
}

Answer №1

There seems to be an issue with your syntax. Please refer to my comment on your code below. It appears that you have not properly closed the function after return true; and instead you are defining the next function within the first one.

function jsDecimals(e) {

  var evt = (e) ? e : window.event;
  var key = (evt.keyCode) ? evt.keyCode : evt.which;
  if (key != null) {
    key = parseInt(key, 10);
    if ((key < 48 || key > 57) && (key < 96 || key > 105)) {
      if (!jsIsUserFriendlyChar(key, "Decimals")) {
        return false;
      }
    } else {
      if (evt.shiftKey) {
        return false;
      }
    }
  }
  return true; // <-- why is there no closing bracket after this line?

  function jsIsUserFriendlyChar(val, step) {
  .
  .
  .

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

Discover the method for filling a ListBox in ASP .NET without triggering a PostBack

Within my Web Form, I have two Listboxes named lstbx01 and lstbx02. The first listbox, lstbx01, is connected to sqlDataSource01 and fills up during the Page Load Event. On the other hand, lstbx02 should populate based on the selected value of lstbx01 as a ...

AngularJS: Click on image to update modelUpdate the model by clicking

I am a newcomer to AngularJS and I am attempting to update my model after the user clicks on an image. Below is the code for this: <div class="col-xs-4 text-center"><a ng-model="user.platform" value="ios"><img src="ios.png" class="img-circl ...

Adding property to an object retrieved from an API in a React application can be achieved effortlessly by utilizing the useState

How can I implement a toggle functionality for Bookmarked Meals on my Meal Recipe Website available at ? I am currently using the logic to set data.meals[0].bookmarked to true or false, but I want to use setState instead in order to rerender the page when ...

Exploring Node.js and Express routing: Strategies for managing client-side promises

I've been working on a routing issue with MongoDB where I'm having trouble retrieving data on the client side from the promise received via $resource. There's a button on the HTML page that triggers the following function on ng-click: $scop ...

AngularJS Vimeo API Request Error: "401 Authorization Required"

I've been experimenting with making external API calls to Vimeo from my AngularJS code using $http.jsonp. However, I keep receiving a 401 Authorization required error even though I have included my authorization key in the header. I encountered a simi ...

Retrieving Values from Array with AngularJS Keys - A How-To Guide

I need to access a specific value from a key inside an array, like this: $scope.result = [ { "key":"logo_big", "value":"/assets/images/aaa.jpg" }, { "key":"logo_small", "value":"/assets/images/logo94x57Bis.png" }, { ...

Dealing with errors in Node.js using the Express framework and the

The code I'm having trouble with is shown below app.get('/', function(req, res, next) { if (id==8) { res.send('0e'); } else { next(); } }); app.use(function(err, req, res, next){ res.send(500, ' ...

When using jQuery, hover over an li element while ignoring its children

I'm working on customizing a Wordpress menu that has a submenu. I managed to add an arrow to the 'li' element that contains a submenu, and created a hover animation with CSS when the mouse moves over the 'a' tag inside the 'li ...

Vue.js is unable to dispatch an event to the $root element

I'm having trouble sending an event to the root component. I want to emit the event when the user presses enter, and have the root component receive it and execute a function that will add the message to an array. JavaScript: Vue.component('inp ...

Submitting a form from a non-AngularJS application to an AngularJS app requires URL rewriting

I am facing a challenge with my AngularJS search application. The search box is located on a standard HTML page that is not part of the AngularJS framework. Here is how the input field looks: <form accept-charset="UTF-8" name="search" id="search" act ...

Encountering a 415 Unsupported Media Type error when making a post request to an ASP.NET Core API from an Angular application

Utilizing the code above in an Angular application: function Login(email, password, callback) { var GetAll = new Object(); GetAll.email = email; GetAll.password = email; $http({ url: "http://local ...

What are some ways to shorten the Xpath for improved efficiency?

Currently, I am attempting to fetch the value of a menu item in an Angular website. By utilizing a detailed Xpath, I have been able to successfully select the desired menu item from the drop-down menu. Is there a way to shorten the length of the XPath sig ...

Adjust the width of your table content to perfectly fit within the designated width by utilizing the CSS property "table width:

Example of a table <table> <tr> <td>Name</td> <td>John</td> <td>Age</td> <td>25</td> <td>Job Title</td> <td>Software Engineer ...

Turn off pagination for tables in Material-UI

Currently, I am utilizing the TablePagination component from Material-UI in my React project. Unfortunately, this component does not come with a built-in disabled prop. I do have a boolean variable called loading that I would like to utilize as a paramet ...

Uncovering the array within Javascript

I'm struggling to figure out why I can't access an array that PHP sends back to me. When AJAX makes a call, I send back this: $response['success'] = true; In my PHP file, I use echo json_encode($response); to send it. However, when I ...

Deliver JSON from Node.js to the client

I am a beginner in the world of Node.js and JavaScript, facing a challenge that I can't seem to overcome. Currently, I have a Node application set up with Express. The issue at hand involves a script that sends JSON data to my server, which is then s ...

The Material-UI selector isn't functioning properly for me

I recently tried to incorporate a material-ui selector example into my project by referring to the code fragment obtained from an example on Github. import React from "react"; import {withStyles} from "material-ui/styles"; import Select from "material-ui/ ...

Tips on separating a function into two separate files in Node.js

Hey there! I am just starting to explore Node.js so bear with me if I make any mistakes. So, I have this file: exports.Client = function() { this.example = function(name, callback) { console.log(name); }; ...

How do popular social media platforms such as Facebook and Twitter utilize real-time APIs to display live updates in news feeds

I'm curious about the technology being used for real-time updates, and I doubt they rely on AJax methods like setInterval() to make server requests every second. This approach could be inefficient with a large number of concurrent users. Do you think ...

Input various colored text within an HTML element attribute

In my asp.net project, I am looking to dynamically change the text color of a table cell based on a certain parameter. Here's an example scenario: TableCell dataCell = new TableCell(); foreach (var o in results) { ...