Birinchi harf alfabasini faqat ma'lum bir kirish maydonida burchaklar yordamida cheklash qanday?

Men matn maydoniga ega bo'lgan <input type = "text" joy hold = "" class = "form-control" ng-model = "firstName"> birinchi belgilar faqat alifbo bo'lishi kerak, foydalanuvchi birinchi alomatni kiritishga harakat qilganda alfavitni qabul qilmaydi.

1

6 javoblar

Birinchi qatorli belgining umumiy regex tekshiruvi.

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var testString= "This is testString"
console.log(testString+" - "+alphabeticalFirstChar(testString));


testString= "1This is testString"
console.log(testString+" - "+alphabeticalFirstChar(testString));

testString= "This is testString0"
console.log(testString+" - "+alphabeticalFirstChar(testString));

testString= ""
console.log(testString+" - "+alphabeticalFirstChar(testString));

function alphabeticalFirstChar(str){
   if(/^[A-z]+$/.test(str[0]) && !str.length<1)//added check for str not being empty - returns true otherwise
      return true;
   else
      return false;
}
</div> </div>
3
qo'shib qo'ydi
Ro'yxatni angularjlar bo'lgan savolga qo'llasangiz yaxshiroq bo'lardi
qo'shib qo'ydi muallif asdf_enel_hak, manba
Ha, lekin ng-modeliga qanday murojaat qilish kerak
qo'shib qo'ydi muallif asdf_enel_hak, manba
Regex to'g'ri!
qo'shib qo'ydi muallif Nirus, manba
@moguzalp angularjs o'zgaruvchan/turli echimlarni taklif qiladimi? Demak, bu yechim menga juda o'xshash ko'rinadi va har qanday JS asosidagi tizim bilan ishlash kerak ... To'g'ri?
qo'shib qo'ydi muallif Adam K., manba
@moguzalp tbh angularjs haqida fon bilimi yo'q ... Kimdir mening umumiy javobimni muayyan yechimga o'zgartirishga qodir. Kechirasiz.
qo'shib qo'ydi muallif Adam K., manba

Quyidagi kodni sinab ko'ring:

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var myApp = angular.module('myApp', []);
myApp.controller('myController', ['$scope', function($scope){
  $scope.firstName = "";
  var regex = /^[A-z]+$/;
  $scope.$watch('firstName', function(newValue, oldValue){
      if($scope.firstName.length > 0){
          if(regex.test(newValue)){
              $scope.firstName = oldValue;
          }
      }
  });      
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myController">
  <input type="text" placeholder="" class="form-control" ng-model="firstName">
</div>
</div> </div>
2
qo'shib qo'ydi

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var myApp = angular.module('myApp', []);
myApp.controller('controller', function($scope) {
        $scope.password;
        $scope.check = function() {
                if($scope.password != null){
                        var charArray = $scope.password.split('');
                        console.log(charArray);
                        if(/[a-zA-Z\_]$/.test(charArray[0]) && charArray[0] != null) {
                                return true;
                        }
                        else {
                                return false;
                        }
                }
        }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
  <div ng-app="myApp" ng-controller="controller">
  <input type="password" ng-model="password" placeholder="Enter Password"/>
        
        
  
 </div>
</div> </div>

Umid qilamanki bu yordam beradi.

1
qo'shib qo'ydi
.split ('') , albatta, kerak emas. String avtomatik ravishda belgilar majmuasi sifatida ishlaydi. - Aks holda, yaxshi javob.
qo'shib qo'ydi muallif Adam K., manba

Html-da

<input type="text" placeholder="" class="form-control" ng-model="firstName" ng-change="checkAlphabet()">

Nazoratchi

$scope.checkAlphabet = function() {
    if($scope.firstName.substr(0, 1).match(/[A-Za-z]/) == null)
        $scope.firstName = $scope.firstName.substr(1);
}
1
qo'shib qo'ydi

Working example using $watch using regex as pointed by @Adam

Follow: https://jsfiddle.net/nirus/3Lytwybr/2/

Javascript:

function LoginController($scope) {
   $scope.user = {
      firstName: ""
   };
   $scope.$watch('user.firstName', function(){
        console.log("changed");
      var cache = $scope.user.firstName;
      if (!(/^[a-zA-Z]+$/.test($scope.user.firstName[0]))){         
        $scope.user.firstName = cache.slice(1,cache.length);
        console.log("here: "+ cache)
      }
   })
}

HTML:

<div ng-app ng-controller="LoginController">
    <div>Hello {{ user.firstName }}</div>
    <input ng-model="user.firstName" />
</div>

Eslatma: Ko'plab tomoshabinlardan foydalanishdan qoching, chunki u ilovaning ishlashiga putur etkazadi.

0
qo'shib qo'ydi

Men Adam K kodini o'zgartirib, burchakka tarjima qildim.

if(/^[A-z]+$/.test($scope.firstname[0]) == false) {
  $scope.firstname = ""; }

Umid qilgandim.

0
qo'shib qo'ydi
Angular Uzbekistan
Angular Uzbekistan
107 ishtirokchilar

Guruh Angular bo'yicha muloqot qilish uchun ochilgan Sizni qiziqtirsa: @nodejs_uz @react_uz @angular_uz @yiiframework_uz @js_uzb @typescript_uzb @vuejs_uz @ngTashkent @laravel_uz @uzdevgroup Ish o'rinlari @UzDev_jobs

Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb

ngTashkent
ngTashkent
77 ishtirokchilar

Правила просты: 1. Не хамить 2. Не хейтить 3. Реакт не нужен Ссылки: https://t.me/angular_ru - старший брат https://t.me/angular_uz - ангулар на узбекском @js_uzb @vuejs_uz @react_uz @nodejs_uz @yiiframework_uz @uzdevgroup @UzDev_Jobs @tasdev_talks