Issue with mobile browser validation for text field functionality not functioning properly

  • Issue with text box validation on mobile browsers for my Angular application
  • Mobile Browsers: Chrome, Samsung Browser (not working in both)
  • Expected Input: Numbers 0-9 and Alphabets A-Z only in the text field

My Approach :

1. Initial Attempt: I attempted to validate using key charCode. It worked on laptop browsers but failed on mobile browsers.

The character code was 229 for every key on phone browsers, hence it did not work.

Code Snippet:

HTML

<input (keypress)="numberOnly($event)" type="text" />

Typescript

numberOnly(event): boolean {
    const charCode = event.which ? event.which : event.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
      return false;
    }
    return true;
}

2. Using Directive: I created a directive to handle validation, which also failed on mobile browsers.

Directive Code:

import { Directive, HostListener, ElementRef, Input } from '@angular/core';

@Directive({
  selector: '[specialIsAlphaNumeric]'
})
export class SpecialCharacterDirective {

  regexStr = '^[a-zA-Z0-9_]*$';
  @Input() isAlphaNumeric: boolean;

  constructor(private el: ElementRef) { }


  @HostListener('keypress', ['$event']) onKeyPress(event) {
    return new RegExp(this.regexStr).test(event.key);
  }

  @HostListener('paste', ['$event']) blockPaste(event: KeyboardEvent) {
    this.validateFields(event);
  }

  validateFields(event) {
    setTimeout(() => {
      this.el.nativeElement.value = this.el.nativeElement.value.replace(/[^A-Za-z ]/g, '').replace(/\s/g, '');
      event.preventDefault();

    }, 100)
  }

}

3. Using Pattern: Even after trying pattern validation, it didn't work on mobile browsers.

Pattern Used: /[A-Z]{5}\d{4}[A-Z]{1}/i

I implemented validation using Validator.pattern in a reactive form.
username: ['', Validators.pattern('/[A-Z]{5}\d{4}[A-Z]{1}/i')],}) 

HTML

<input  formcontrolname='username' (keypress)="numberOnly($event)" type="text" />

Playground : https://codesandbox.io/embed/dazzling-hellman-pygp1?file=/src/app/app.component.html:64-119&codemirror=1

Answer №1

I recently came across some advice stating the importance of ensuring that JavaScript is enabled in the browser you are using.

Check out this forum discussion for more insights.

Answer №2

Ensure that JavaScript is enabled in the settings of your mobile browser!

source: Client side validation issues on mobile devices

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

Pass the JavaScript variable to the Laravel controller for updating VueJS

Trying to send data from my modal to a controller to update data, but I'm not sure how to declare my variable and how to send this variable... Here is my current code: Vue.js <script> export default { data() { return { ...

Creating multiple synchronous loops within a Vue component using JavaScript

I'm dealing with a JavaScript loop that processes data from an Excel file. The loop loops through the results and retrieves a list of PMIDs. If the PMIDList has more than 200 items, it needs to be split into segments of 200 for processing due to restr ...

React: Remember to always retain the initial four characters when making changes

I have implemented an input component for phone numbers using the react native phone input library, which automatically adds the international code. However, I am facing an issue where the international code +234 is deleted when the user presses the back b ...

Show only the portion of the image where the cursor is currently hovering

Information in advance: You can check out the current code or view the live demo (hover image functionality not included) at . The concept: I would like to have it so that when I hover my cursor (displayed as a black circle) over the image, only the s ...

Cannot populate Kendo Menu with images due to dataSource imageUrl not recognizing the content

I have integrated Kendo controls into my application, specifically a Menu control with a datasource in my controller. Currently, I am passing the relative path of URL for imageUrl. However, as the application has grown, there are multiple calls being made ...

What is the best way to redirect to the login page using the PUT method in express.js when the user is not authenticated?

I am encountering an issue where I need to update a database from an AngularJS page using the $http.put method. However, if the session expires, it displays errors on the server. When the put method is triggered, it goes to this route: PUT /api/categorie ...

Possible issue with accurate indexing causing caption error with API images in React

Continuing from: Implementing a lightbox feature in react-multi-carousel for my ReactJS app My application utilizes react-images for the lightbox functionality and react-carousel-images for the carousel. The API provides a title and image data. The issue ...

I am currently facing a challenge in React Highcharts where I am unable to remove and redraw the graph easily

Having an issue where I can't remove and redraw the chart in my React Highchart project. I've been unable to find a solution for this problem. Here is the code snippet: import { useState, useEffect, useRef } from "react"; import Highch ...

Distinguishing div identifiers for jQuery displaying and concealing

Although I am not a coder, I am still learning and trying to improve my skills. I have multiple div elements listed by a MySQL query. I require a JavaScript code that can display the class="qform" div based on which button is clicked. If button-1 is clic ...

What is the best way to eliminate an AngularJS filter from a table?

I'm currently working on a feature that involves generating a table of values using ng-repeat. Within this table, there is also a text box for filtering purposes. Everything is functioning as expected so far. However, I have encountered an issue with ...

Transferring data from a table to another window

Currently, I am retrieving values from a database and displaying them in a table. I would like to add a button to each row that will allow users to view more details about that specific row. At the moment, only ThesisID, AuthorID, and Title are visible. ...

An issue arose while resolving symbol values statically. The function 'ɵmakeDecorator' cannot be called as function calls are not allowed in this context

I have developed a unique "Angular component library" that I refer to as ACL Now, I am incorporating this library into another project. Initially, when I execute ionic-app-scripts build, it completes successfully However, when I run ionic-app-scripts b ...

Tips on sending a list via @Input using Angular5

I was seeking guidance on how to pass a list through an input. In my parent component, I have the following: list: Hero[] = [{Id: 2, Name: "Sll"}, {Id: 3, Name: "Sldsadasd"}] and in the HTML: <app-add-hero list="{{list}}" hero={{hero}}></app-ad ...

Using JavaScript/TypeScript to sort through and separate two arrays

Creating a list of checkboxes on a UI allows users to toggle and filter data results. To achieve this, I am storing the selected checkboxes as a string array. The structure of my code is outlined below: export interface IMyObjectFromAPI { status: { ...

Trouble handling Ajax response

I have two JSP files described below. The parent JSP page contains a Select dropdown box with two options. Another JSP file, dispTable.jsp, displays select boxes inside a table. If the user selects "one" from the dropdown in the parent.jsp page, I want ...

Surprising Results when Using getBoundingClientRect()

Check out this code on CodePen In my current project, I'm attempting to position the footer div at the bottom of the page in such a way that it aligns with the maximum value of the bottom coordinates of both the menu and content divs (Math.max(menu, ...

Changing the border of an iframe element using jQuery or Javascript from within the iframe itself

Is it possible to set the border of an iframe to zero from within the iframe itself using JavaScript or jQuery? Any guidance on how this can be achieved would be greatly appreciated. Thank you! ...

Transforming a text file into an array using fs in Node.js

My text file contains the following: {"date":"2013/06/26","statement":"insert","nombre":1} {"date":"2013/06/26","statement":"insert","nombre":1} {"date":"2013/06/26","statement":"select","nombre":4} Is there a way to convert the text file contents ...

Is there a way to give a model of a directive scope a dynamic name while also enabling 2-way binding?

I have a challenge where I need to dynamically pass object values into a directive using ng-repeat. Here's what I've attempted so far: <td ng-repeat="attr in demographicAttrs"> <yes-no name="{{ attr }}-{{ $index }}" model="ambassa ...

Guide to uploading a JavaScript File object to Cloudinary via the node.js API

After researching various options, I decided to use cloudinary for uploading a file to an image server from my node js api. I successfully installed the npm package for cloudinary and implemented the code based on their api documentation Below is the fun ...