在字符串中计算逗号 [英] Counting commas within a string

查看:133
本文介绍了在字符串中计算逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好:


我有一个包含多选字段的表单,其中有12个

选项。我希望用户能够选择最多四个这样的选项
。如果他们选择了超过四个,我想提醒他们错误。要做到这一点,我认为计算逗号将是最简单的方法(例如,IF逗号> 3那么警告用户)。


注意:我有一个现有的validateForm这个表格的功能和我要

喜欢将这个IF-THEN语句添加到它。


非常感谢任何帮助。此外,如果有一个更好的方法

而不是逗号计数我很满意。 :)


谢谢,

Mike

解决方案

* Mike N. *在comp.lang.javascript中写道

您好:

我有一个包含多选字段的表单,其中包含12
选项。我希望用户能够选择最多四个这些选项。如果他们选择了四个以上,我想提醒他们这个错误。为此,我认为计算逗号将是最简单的方法(即,IF逗号> 3那么警告用户)。

注意:我有一个现有的validateForm。这个表格的功能我想要将这个IF-THEN语句添加到它。

非常感谢任何帮助。此外,如果有比逗号计数更好的方法
我很满意。 :)




假设''strValue''是您在上面描述的CSV字符串,那么:


var intNumFields = strValue .split(",")。length;


这个值是你选择的数量,而不是它们之间逗号的数字




//例如:

var MAXFIELDS = 4;


if(intNumFields> MAXFIELDS){

提醒(对不起,请选择最多+ MAXFIELDS +"

选项)

返回false;

}

-

Andrew Urquhart

- 常见问题: http://www.jibbering.com/faq/

- 存档: http://groups.google.com/groups?grou...ang。 javascript

- 与我联系: http://andrewu.co.uk/contact/


感谢您的快速回复!

不幸的是,它不适合我。基本上,无论我选择多少(一个,五个或全部),它都会跳过这段代码(没有发现错误我猜了)。


你确定这一行是正确的吗?


var intNumFields = strValue.split(",")。length;

我不熟悉.length它的一部分。


再次感谢!


***通过Developersdex发送 http://www.developersdex.com ***

不要只是参加USENET ......获得奖励对于它!


* Mike N. *在comp.lang.javascript中写道

感谢您的提示回复!

不幸的是,这对我不起作用。基本上,无论我选择了多少(一个,五个或全部),它都会跳过这段代码(没有找到我猜错的错误)。



[ snip]


请发布一个不起作用的示例URI - 可能还有另一个

问题。

-

Andrew Urquhart

- 常见问题: http://www.jibbering.com/faq/

- 存档: http://groups.google.com/groups?grou...ang.javascript

- 与我联系: http://andrewu.co.uk/contact/


Hello:

I have a form that contains a multiple-select field that has 12
options in it. I would like the user to be able to select UP TO FOUR
of those options. If they select more than four, I would like to alert
them of the error. To do this, I figure that counting commas would be
the easiest method (i.e., IF commas > 3 THEN alert user).

NOTE: I have an existing "validateForm" function for this form and I''d
like to add this IF-THEN STATEMENT to it.

Any assistance is much appreciated. Also, if there''s a better method
than "comma counting" I''m all ears. :)

Thanks,
Mike

解决方案

*Mike N.* wrote in comp.lang.javascript:

Hello:

I have a form that contains a multiple-select field that has 12
options in it. I would like the user to be able to select UP TO FOUR
of those options. If they select more than four, I would like to alert
them of the error. To do this, I figure that counting commas would be
the easiest method (i.e., IF commas > 3 THEN alert user).

NOTE: I have an existing "validateForm" function for this form and I''d
like to add this IF-THEN STATEMENT to it.

Any assistance is much appreciated. Also, if there''s a better method
than "comma counting" I''m all ears. :)



Assuming ''strValue'' is the CSV string you describe above, then:

var intNumFields = strValue.split(",").length;

This value is the number of selections you have, rather than the number
of commas between them.

// E.g.:
var MAXFIELDS = 4;

if (intNumFields > MAXFIELDS) {
alert("Sorry, please select up to a maximum of " + MAXFIELDS + "
choices")
return false;
}
--
Andrew Urquhart
- FAQ: http://www.jibbering.com/faq/
- Archive: http://groups.google.com/groups?grou...ang.javascript
- Contact me: http://andrewu.co.uk/contact/


Thanks for the prompt response!

Unfortunately, it''s not working for me. Basically, no matter how many I
select (one, five, or all) it skips this code (not finding an error I
guess).

Are you sure this line is correct?

var intNumFields = strValue.split(",").length;

I am not familiar with the ".length" portion of it.

Thanks again!

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!


*Mike N.* wrote in comp.lang.javascript:

Thanks for the prompt response!

Unfortunately, it''s not working for me. Basically, no matter how many I
select (one, five, or all) it skips this code (not finding an error I
guess).


[snip]

Please post an example URI of it not working - there''s likely another
problem.
--
Andrew Urquhart
- FAQ: http://www.jibbering.com/faq/
- Archive: http://groups.google.com/groups?grou...ang.javascript
- Contact me: http://andrewu.co.uk/contact/


这篇关于在字符串中计算逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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