2個javascript小問題

2007-03-04 4:21 pm
1.
如何用javascript寫一個function
當我按一個button時
可以check到在textbox輸入的是否數字
例如要輸入電話號碼, 我需要確認輸入的數值不包含英文字母
(就好似 VB 既 isNumeric 咁)

2.
假如我有2個checkbox
當我按一個button時
佢可以check到這2個checkbox 最少要剔其中一個,或2個都剔晒
如果2個checkbox都無剔就彈個alert出黎

回答 (1)

2007-03-04 4:45 pm
✔ 最佳答案
1/
Write a short function to go through each character in the string and see if they are ALL valid.

function IsNumeric(sText)

{
var ValidChars = "0123456789.";
var IsNumber=true;
var Char;


for (i = 0; i < sText.length && IsNumber == true; i++)
{
Char = sText.charAt(i);
if (ValidChars.indexOf(Char) == -1)
{
IsNumber = false;
}
}
return IsNumber;

}

2007-03-04 08:50:10 補充:
2/ function check(field) {var atLeastOneChecked = false;for (i = 0; i < field.length; i++) { if (field[i].checked) { atLeastOneChecked = true break }}if (!atLeastOneChecked){ alert("You need to check at least 1 box!!")}}


收錄日期: 2021-04-23 21:00:45
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070304000051KK00766

檢視 Wayback Machine 備份