简体   繁体   中英

jQuery custom validation rule question

I have a random math generator and a .get command that sets the value from the returned data. For instance the input #Captcha would get it's value as such:

$.get('PHP/random.php',
    function(data) {
    $("#Captcha").val(data);
});

If the returned data was something like 2 + 5 how would you create a rule that turns the value into two integers, strips out the "+" and then combines the two integers?

Not sure if this is exactly what you're looking for. But if you just to add the two (or more) numbers together this should work:

var test = "2+5";
var numbers = test.split('+');
var result = 0;
for (var i = 0; i < numbers.length; i++){
    result += parseInt(numbers[i]);
}

Working example - http://jsfiddle.net/L8VMH/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM