验证在Phonegap中不起作用,但在浏览器中可完美运行 [英] Validation is not working in Phonegap but works perfectly in Browser

查看:76
本文介绍了验证在Phonegap中不起作用,但在浏览器中可完美运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个number类型的字段.我已经使用jquery验证了此字段,该字段在浏览器中显示了必要的警告,但在与Phonegap结合使用时在电话中却无法显示.

I have a field of type number . I have validated this field using jquery it shows necessary warnings in browser but failed to show in phone when using it with Phonegap.

我的代码是:

PUT YOUR ANSWER HERE: <input id="ans" type="number" name="ans" min="1" max="10">

  <input id="nextbutton1" type="button" name="next1" value="Subbmit" class="nextbutton">

我只接受1到10之间的数字.但是,当我输入1--或1 2或2 ++时,它将接受输入而电话中不会出现任何提示.

I want to accept only numbers from 1 to 10. But when I am entering 1-- or 1 2 or 2++ it takes input without any prompt in phone.

我的jquery是这个

My jquery is this

$('#nextbutton1').click(function() {
   var num=$("#ans").val().trim();

                if ((num < 1 || num > 10) && num.length != 0) {
        if (num < 1) {
            alert("Oops! You cannot enter vlues smaller than 1");
          $('input[type=number]').val('');

        }

        if (num > 10) {
            alert("Oops! You cannot enter vlues Greater than 10");
            $('input[type=number]').val('');
        }

    }

else if ((num % 1) != 0) {
    alert("You Cannot Enter Decimal Values or any other symbols ");
    $('input[type=number]').val('');
}
else if(num=="")
{
alert("Oops ! Please Select any number from 1 to 10 ");
$('input[type=number]').val('');
}
else
{
if(num == rand)
{
alert("Correct");
}
}
 });

此处rand是从1到10的随机生成的数字.

Here rand is random generated number from 1 to 10.

我该怎么做才能在电话中完美工作?

What can i do to work perfectly in phone?

更新

我也已经按照此答案中的建议进行了此更正,但仍然不起作用.

I have also done this correction as suggested in this answer but still not working.

推荐答案

在将其与数字进行比较之前,需要将值 string 转换为整数.因此正确的行将是:

You need to convert value string to integer before comparing it with numbers. So correct line would be:

var num = window.parseInt($("#ans").val().trim(), 10);

这篇关于验证在Phonegap中不起作用,但在浏览器中可完美运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆