Javascript用于添加2个数字的验证。 [英] Javascript for validation on addition of 2 numbers.

查看:89
本文介绍了Javascript用于添加2个数字的验证。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我想要合并将检查2个文本框的添加的功能不应该大于100.

Plz帮帮我.... .......

谢谢...........



我尝试了什么:



我试图提供以下功能

Hello,
I wanted to incorporate the function that will check the addition of 2 textbox should not be greater than 100.
Plz help me out...........
Thanks...........

What I have tried:

I have tried to put the following function

function foo
		{
		if(txt3.val>100)
		{ print('Enter valid number');
		}
		else
		{
			print('OK');
		}
		}

推荐答案

我在第一行可以看到一个错误:该函数应该是 function foo()



此外,你通过 txt3 通过外部环境,这是坏事。功能应尽可能从上下文中抽象出来;这就是他们可以争论的原因。 (我不知道 val 是什么,HTML元素的属性,但让我们假设你正确添加了一个有效的属性并正确地赋值。)

txt3.val 可能是一个不代表任何有效数字的字符串;然后它会工作,但在所有情况下都返回false。也许你需要防止这种情况(或者不是,如果数字已经是数字运算的结果)。



在任何情况下,它都更好,在数字函数中,使用数字,而不是像 txt3 这样的对象,它们可能会也可能不会提供数字数据。如果对象与某些HTML元素相关,则最好将UI与计算分开。你应该给对象语义敏感的名字,而不是像txt3那样。您的命名使您的代码无法维护。



-SA
I can see a bug in first line: the function should be function foo().

Besides, you pass txt3 through the outside context, which is bad. Functions should be abstracted from context as much as possible; that's why they can have arguments. (I don't' know what val is, HTML elements have the property value, but let's assume that you correctly added a valid property and assigned it a value correctly.)
It possible that txt3.val is a string not representing any valid number; then it would work, but returning false in all cases. Perhaps you need to protect against such cases (or not, if the number is already the result of numeric operation).

In all cases, it's better, in numeric functions, to work with numbers, not with some objects like txt3 which may or may not provide numeric data. If the object is related to some HTML element, it's better to separate UI from calculations. And you should give objects semantically-sensitive names, never anything like "txt3". Your naming makes your code not maintainable.

—SA


这篇关于Javascript用于添加2个数字的验证。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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