我正在尝试使用jQuery获取字段中的字母和数字的数量。
当前我正在使用此代码,但它不起作用:
$("#sPassword1").on('input', function() {
var fieldValue = $(this).val();
var count = fieldValue.length;
var countL = fieldValue.match(/([A-z])/g).length;
var countN = fieldValue.match(/([0-9])/g).length;
console.log(count);
console.log(countL);
console.log(countN);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='password' id='sPassword1' value='abc12'/>
我在做什么错? :(
答案 0 :(得分:1)
这对我有用,似乎唯一缺少的就是在没有匹配项的情况下进行处理-匹配项不会返回空数组,而是返回空值。请参阅RegExp行:
$("#sPassword1").on('input', function() {
var fieldValue = $(this).val();
var count = fieldValue.length;
var countL = (fieldValue.match(/([A-z])/g) || []).length;
var countN = (fieldValue.match(/([0-9])/g) || []).length;
console.log({ count, countL, countN, fieldValue });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="sPassword1" />
答案 1 :(得分:1)
您忘记控制match函数返回null。
$( "#sPassword1" ).on('input', function() {
var fieldValue = $(this).val();
var count = fieldValue.length;
var match1= fieldValue.match(/([A-z])/g);
var match2= fieldValue.match(/([0-9])/g);
var countL = 0;
var countN = 0;
if ( match1 != undefined ){
countL = match1.length;
}
if ( match2 != undefined ){
countN = match2.length;
}
console.log(countL + " " +countN );
});
答案 2 :(得分:1)
就这样
<?php
namespace App\Tests\Service;
use App\Service\MyClassService;
use App\Service\ExternalClient\ExternalClient;
use JMS\Serializer\Serializer;
use JMS\Serializer\SerializerInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\Request;
class MyClassServiceTest extends KernelTestCase
{
/** @var LoggerInterface */
private $logger;
/** @var Serializer */
private $serializer;
/** @var ExternalClient */
private $externalClient;
/** @var RequestInterface */
private $request;
/** @var MyClassService */
private $myClassService;
public function setUp()
{
$kernel = self::bootKernel();
$this->logger = $kernel->getContainer()->get(LoggerInterface::class);
$this->serializer = $kernel->getContainer()->get(SerializerInterface::class);
$this->externalClient = $this->createMock(ExternalClient::class);
}
public function testPassRegistrationData()
{
$getParams = [
'amount' => '21.56',
'product_id' => 867,
'order_id' => '47t34g',
'order_item_id' => 2,
'email' => 'kiki%40bubu.com',
];
$this->generateMyClassService($getParams);
$userInformation = $this->myClassService->passRegistrationData();
var_dump($userInformation);
}
/**
* generateMyClassService
*
* @param $getParams
*
* @return MyClass
*/
private function generateMyClassService($getParams)
{
$this->request = new Request($getParams, [], [], [], [], [], null);
$this->myClassService = new MyClassService(
$this->logger,
$this->serializer,
$this->externalClient,
$this->request
);
}
}
Symfony\Component\DependencyInjection\Exception\RuntimeException: Cannot autowire service "App\Service\MyClassConfirmationService": argument "$request" of method "__construct()" references class "Symfony\Component\HttpFoundation\Request" but no such service exists.