Edit控件只能输入数字
procedure Tform1.edit1(Sender: TObject;
var Key: Char);
begin
If ((key < '0') Or ( key > '9')) and (key <> chr(8)) and (key <> '.') then
key := #0;
if key = '.' then
if AnsiContainsStr((Sender as tedit).Text , '.') then
key := #0;
end;//---------------------------------------------------
判断字符串是否为数字
uses IdGlobal;function TForm1.StrIsNumber( str : String) : Boolean;
var
p : pchar;
i : integer;
c : char;
strVal,strTemp : string;
begin
strVal := Trim(str); if Length(strVal)<=0 then
begin
Result := False;
exit;
end; for i := 0 to length(strVal) - 1 do
begin
p := Pchar(strVal);
c := p[i];
if not IsNumeric(c) then
begin
SetLength(strTemp,1);
strTemp :='.';
if strTemp <> c then
begin
Result := False;
exit;
end;
end;
end; Result := True;
end;
//-----------------------------
//字符串是否整形
function IsInt(AStr: string): Boolean;
var
Value, Code: integer;
begin
Val(AStr, Value, Code);
Result := Code = 0;
end;//字符串是否实型
function IsFloat(AStr: string): Boolean;
var
Value: Real;
Code: integer;
begin
Val(AStr, Value, Code);
Result := Code = 0;
end;
procedure Tform1.edit1(Sender: TObject;
var Key: Char);
begin
If ((key < '0') Or ( key > '9')) and (key <> chr(8)) and (key <> '.') then
key := #0;
if key = '.' then
if AnsiContainsStr((Sender as tedit).Text , '.') then
key := #0;
end;//---------------------------------------------------
判断字符串是否为数字
uses IdGlobal;function TForm1.StrIsNumber( str : String) : Boolean;
var
p : pchar;
i : integer;
c : char;
strVal,strTemp : string;
begin
strVal := Trim(str); if Length(strVal)<=0 then
begin
Result := False;
exit;
end; for i := 0 to length(strVal) - 1 do
begin
p := Pchar(strVal);
c := p[i];
if not IsNumeric(c) then
begin
SetLength(strTemp,1);
strTemp :='.';
if strTemp <> c then
begin
Result := False;
exit;
end;
end;
end; Result := True;
end;
//-----------------------------
//字符串是否整形
function IsInt(AStr: string): Boolean;
var
Value, Code: integer;
begin
Val(AStr, Value, Code);
Result := Code = 0;
end;//字符串是否实型
function IsFloat(AStr: string): Boolean;
var
Value: Real;
Code: integer;
begin
Val(AStr, Value, Code);
Result := Code = 0;
end;
function is_number(str)
{
exp=/[^0-9()-]/g;
if(str.search(exp) != -1)
{
return false;
}
return true;
}
function check(){
if(!is_number(form1.text1.value)){
return false;
}
return true;
}
</script>
<form name=form1 onsubmit=return(check())>
<input type=text name=text1>
<input type=submit>
</form>
<script language="VBScript"><!--
Sub Submit_OnClick
Dim TheForm
Set TheForm = Document.ValidForm
If IsNumeric(TheForm.Text1.Value) and isnumeric(theform.text2.value) Then
MsgBox "请输入一个数字。"
End If
End Sub--></script>
{
if ( !(((window.event.keyCode >= 48) && (window.event.keyCode <= 57))
|| (window.event.keyCode == 13) || (window.event.keyCode == 46)
|| (window.event.keyCode == 45)))
{
alert("对不起,只能输入数字")
window.event.keyCode = 0 ;
}
}
<input name="text" type="text" onKeyPress="OnlyNumberText()">