angular half-width number detection

Asked 2 years ago, Updated 2 years ago, 85 views

anglerJS v1.5.8

■ Questions
Learn how to validate input values

■What you want to do
Avoid mixing half-width numbers in a string
Show error message if mixed

■ Current HTML

<input required class="form-control" id="name" name="name" type="text"ng-model="name"value="ng-pattern="????/"/> 
<div ng-show="myForm.name.$invalid&&!myForm.name.$untouched&myForm.name.$error.pattern"> 
<span class="sign">Mixed numbers</span> 
</div>

■Current state
The input tag can be any character except half-width numbers.
The validation above is not working well.

How can I detect the mix of half-width numbers?

javascript angularjs

2022-09-30 17:41

1 Answers

You can change ng-pattern="????/" to ng-pattern="/^\D+$/".
^ —From the beginning
$ —Until the end
\D —Non-numeric characters
+ —Continuous

Example:

<htmlng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
<body>
<form name="myForm">
  <input required class="form-control" id="name" name="name" type="text"ng-model="name" value=""" 
  ng-pattern="/^\D+$/"/>
  <div ng-show="myForm.name.$invalid&&!myForm.name.$untouched&myForm.name.$error.pattern"> 
  <span class="sign">Mixed numbers</span> 
  </div>
</form>
</body>
</html>


2022-09-30 17:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.