如何在离子1中仅允许一个小数点输入类型编号

时间:2017-09-15 06:20:17

标签: javascript html angularjs regex ionic-framework

我正在使用类型编号的输入,它允许多个小数点,所以我尝试使用正则表达式不允许超过一个小数点,但即使使用正则表达式我面临相同的问题,任何人都可以告诉我如何在ionic1

中输入类型编号只允许一个小数点

HTML:

<input stopccp focus-me class="inputContainer trade_input" type="number" name="" ng-model="vm.total_amount[$index]" ng-change="vm.onTotalCost()" limit-char limit="5" ng-keyup="vm.decimalcheck(vm.total_amount[$index])"  >

功能正则表达式:

function decimalcheck (element) {
  $log.log('decimalcheck got called', element);
  var regex = /\d*\.?\d?/g;
  return regex.exec(element);
}

4 个答案:

答案 0 :(得分:1)

<强>脚本

<input type="text" class="number" />

<强> HTML

<p style="position: relative;">
  <img ....>
  <a style="position: absolute; top: 10%; left: 10%; width: 30%; height: 15%" href=....></a>
</p>

Here is your answer

答案 1 :(得分:1)

function SendTestEmail(EmailAddress: String): Boolean;
var
  EmailMessage: TidMessage;
  IdSSLHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
  IdSMTPEmail.AuthType := satDefault;

  IdSSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create;
  IdSSLHandler.SSLOptions.Method := sslvTLSv1_2;

  IdSMTPEmail.IOHandler := IdSSLHandler;
  IdSMTPEmail.UseTLS := utUseExplicitTLS;

  IdSMTPEmail.Username := ...;
  IdSMTPEmail.Password := ...;
  IdSMTPEmail.Port := 465;

  try
    // Connect
    IdSMTPEmail.Connect('smtp.gmail.com');

    try
      // Create
      EmailMessage := TidMessage.Create(nil);

      try
        // Set values
        EmailMessage.Body.Add('Test Email');
        EmailMessage.Subject := 'Test Email';

        // Set sender details
        EmailMessage.From.Address := 'test@test.com';
        EmailMessage.From.Name := 'SSL Test';

        // Set recipient
        EmailMessage.Recipients.Add.Address := EmailAddress;

        try
          // Send message
          IdSMTPEmail.Send(EmailMessage);

        except
          // Exception
          on E: EIdSMTPReplyError do
          begin
            // Result
            Result := False;
          end;
        end;

      finally
        // Free email
        EmailMessage.Free;
      end;

    finally
      // Disconnect
      IdSMTPEmail.Disconnect;
    end;

  except
    // Exception
    on E: Exception do
    begin
      IdSMTPEmail.Disconnect;
      // Result
      Result := False;
    end;
  end;
end;

答案 2 :(得分:1)

我想你只是错过了开始(^)和结束($)运算符

function decimalcheck (element) {
  $log.log('decimalcheck got called', element);
  var regex = /^\d*\.?\d?$/g;
  return regex.exec(element);
}

答案 3 :(得分:1)

试试这个正则表达式:

^\d*\.?\d+$

Click for Demo

<强>解释

  • ^ - 字符串开头
  • \d* - 0+位数
  • \.? - 0或1小数点
  • \d+ - 1+位数(因此必须在小数点后至少有一位数,如果存在的话)
  • $ - 字符串结尾

<强>输出: enter image description here