【已解决】Input 如何只能输入正数,可以是小数
本帖最后由 touch_xu 于 2011-9-21 20:46 编辑如题,在论坛找了下,没有结果:
GUICtrlCreateInput("", 79, 122, 80, 31,$ES_NUMBER) 这样的话 0124 这样的数是可以接受的
但是56.5这样的数是不能接受的,我要的所以的正数都可以接受,如何实现,谢谢! 借用xayle 的帖子修改给你!#include <GUIConstantsEx.au3>
$Form = GUICreate("Form1", 633, 454, 192, 114)
$Inputuser = GUICtrlCreateInput("", 82, 90, 93, 20)
$Label = GUICtrlCreateLabel("", 186, 91, 160, 20)
$ccccc = GUICtrlCreateButton("Check", 100, 150, 75, 25)
GUISetState(@SW_SHOW)
While 1
$msg = GUIGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
Exit
Case $msg = $Inputuser
InputCheck()
EndSelect
WEnd
Func InputCheck()
$user = GUICtrlRead($Inputuser)
If Not StringRegExp($user, "^[.0-9]+$") Then
GUICtrlSetState($Inputuser, $GUI_FOCUS)
GUICtrlSetData($Label, "X 只允许输入.和数字")
GUICtrlSetColor($Label, 0xFF0000)
GUICtrlSetFont($Label, 9, 400, 0, "Tahoma")
Else
GUICtrlSetData($Label, "√ 输入正确.")
GUICtrlSetFont($Label, 9, 800, 0, "Tahoma")
GUICtrlSetColor($Label, 0x008000)
GUICtrlSetFont($Label, 9, 400, 0, "Tahoma")
EndIf
EndFunc ;==>InputCheck 楼上是使用正则的,学习的了! 借用xayle 的帖子修改给你!
gzh888666 发表于 2011-9-13 00:20 http://www.autoitx.com/images/common/back.gif
十分感谢,但是0012这样的数字仍然接开受,以0开头的数是不合法的,如何解决,谢谢 本帖最后由 gzh888666 于 2011-9-20 13:08 编辑
回复 4# touch_xu #include <GUIConstantsEx.au3>
$Form = GUICreate("Form1", 633, 454, 192, 114)
$Inputuser = GUICtrlCreateInput("", 82, 90, 93, 20)
$Label = GUICtrlCreateLabel("", 186, 91, 160, 20)
$ccccc = GUICtrlCreateButton("Check", 100, 150, 75, 25)
GUISetState(@SW_SHOW)
While 1
$msg = GUIGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
Exit
Case $msg = $Inputuser
InputCheck()
EndSelect
WEnd
Func InputCheck()
$user = GUICtrlRead($Inputuser)
$one = StringLeft($user,1)
If $one ="0" Or $one="."Then
GUICtrlSetData($Label, "首位不能为0或.")
ElseIfNot StringRegExp($user, "^[.0-9]+$")Then
GUICtrlSetState($Inputuser, $GUI_FOCUS)
GUICtrlSetData($Label, "X 只允许输入.和数字")
GUICtrlSetColor($Label, 0xFF0000)
GUICtrlSetFont($Label, 9, 400, 0, "Tahoma")
Else
GUICtrlSetData($Label, "√ 输入正确.")
GUICtrlSetFont($Label, 9, 800, 0, "Tahoma")
GUICtrlSetColor($Label, 0x008000)
GUICtrlSetFont($Label, 9, 400, 0, "Tahoma")
EndIf
EndFunc ;==>InputCheck 回复touch_xu
gzh888666 发表于 2011-9-20 12:47 http://www.autoitx.com/images/common/back.gif
十分感谢,问题已经解决,如果能直接把
0012434 替换为 12434 ,不论开始有多少个0
... 123434 替换为 替换为,不论开始有多少个.
其它非法字符换为空白,就更加完美了,再次感谢 本帖最后由 tsui 于 2011-9-20 21:59 编辑
其实上面的reg是不严谨的,可以多个点 其实上面的reg是不严谨的,可以多个点
tsui 发表于 2011-9-20 21:55 http://www.autoitx.com/images/common/back.gif
所以我要的就是一个合法的正数,当然不要
111...333
111.3333.4444
也不要负数了,继续求教。 本帖最后由 gzh888666 于 2011-9-21 01:03 编辑
回复 6# touch_xu #include <GUIConstantsEx.au3>
$Form = GUICreate("Form1", 633, 454, 192, 114)
$Inputuser = GUICtrlCreateInput("", 82, 90, 93, 20)
$Label = GUICtrlCreateLabel("", 186, 91, 160, 20)
$ccccc = GUICtrlCreateButton("Check", 100, 150, 75, 25)
GUISetState(@SW_SHOW)
While 1
$msg = GUIGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
Exit
Case $msg = $Inputuser
InputCheck()
EndSelect
WEnd
Func InputCheck()
$user = GUICtrlRead($Inputuser)
StringStripWS($user, 8)
$one = StringLeft($user, 1)
If Not StringRegExp($user, "^[.\d]+$") Then
GUICtrlSetState($Inputuser, $GUI_FOCUS)
GUICtrlSetData($Label, "X 只允许输入.和数字")
GUICtrlSetColor($Label, 0xFF0000)
GUICtrlSetFont($Label, 9, 400, 0, "Tahoma")
Else
If $one = "0" Or $one = "." Then
$one2 = StringRegExp($user, "+?(.+)", 1)
GUICtrlSetData($Inputuser, $one2)
EndIf
GUICtrlSetData($Label, "√ 输入正确.")
GUICtrlSetFont($Label, 9, 800, 0, "Tahoma")
GUICtrlSetColor($Label, 0x008000)
GUICtrlSetFont($Label, 9, 400, 0, "Tahoma")
EndIf
EndFunc ;==>InputCheck这个就可以自动删除开头的“0.”,多个点的不知道你需要不,没写!如果需要判断一下.的数量为<=1就行,很简单! 本帖最后由 afan 于 2011-9-20 23:25 编辑
GUICreate('')
$Input = GUICtrlCreateInput('', 100, 100, 100, 20)
$Label = GUICtrlCreateLabel('', 210, 105, 80, 20)
$Bt = GUICtrlCreateButton('Check', 100, 150, 75, 25)
GUISetState()
While 1
$msg = GUIGetMsg()
Switch $msg
Case -3
Exit
Case $Bt
InputCheck()
EndSwitch
WEnd
Func InputCheck()
If StringRegExp(GUICtrlRead($Input), '^\d*(?:\.\d+)?$|^0\.\d+$|^0$') Then
GUICtrlSetData($Label, '√ 输入正确.')
Else
GUICtrlSetData($Label, '× 输入错误.')
EndIf
EndFunc ;==>InputCheck
本帖最后由 learn321 于 2011-9-20 23:33 编辑
借用2楼代码,稍作修改给你,看看行不行?
#include <GUIConstantsEx.au3>
$Form = GUICreate("Form1", 633, 454, 192, 114)
$Inputuser = GUICtrlCreateInput("", 82, 90, 93, 20)
$Label = GUICtrlCreateLabel("", 186, 91, 160, 20)
$ccccc = GUICtrlCreateButton("Check", 100, 150, 75, 25)
GUISetState(@SW_SHOW)
While 1
$msg = GUIGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
Exit
Case $msg = $Inputuser
InputCheck()
EndSelect
WEnd
Func InputCheck()
$user = GUICtrlRead($Inputuser)
; $one = StringLeft($user,1)
;If $one ="0" Or $one="."Then
; GUICtrlSetData($Label, "首位不能为0或.")
IfNot StringRegExp($user, "^[\d]*[.]?[\d]*$")Then
GUICtrlSetState($Inputuser, $GUI_FOCUS)
GUICtrlSetData($Label, "X 只允许输入一个.和数字")
GUICtrlSetColor($Label, 0xFF0000)
GUICtrlSetFont($Label, 9, 400, 0, "Tahoma")
Else
GUICtrlSetData($Label, "√ 输入="&Number($user))
GUICtrlSetFont($Label, 9, 800, 0, "Tahoma")
GUICtrlSetColor($Label, 0x008000)
GUICtrlSetFont($Label, 9, 400, 0, "Tahoma")
EndIf
EndFunc ;==>InputCheck一刷新,afan大佬就站我前面喽。。。与老大近距离接触啊!
最近正学正则,afan大佬的帖子让我受益匪浅啊,非常非常感谢喽,嘿嘿 #include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("Form1", 629, 106, 192, 132)
$Input1 = GUICtrlCreateInput("Input1", 40, 32, 433, 24)
$Button1 = GUICtrlCreateButton("Button1", 496, 32, 105, 33)
GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
GUICtrlSetData($Input1, '')
EndSwitch
WEnd
Func MY_WM_COMMAND($hWnd, $msg, $wParam, $lParam)
Local Const $EN_CHANGE = 0x300
Local $nNotifyCode = BitShift($wParam, 16)
Local $nID = BitAND($wParam, 0xFFFF)
Local $hCtrl = $lParam
If $nID = $Input1 Then
If $nNotifyCode = $EN_CHANGE Then
$read=GUICtrlRead($Input1)
If String(StringLeft($read,1))=="0" Or String(StringLeft($read,1))=="." Then
GUICtrlSetData($Input1,StringRegExpReplace($read,'^0+|^\.+',''))
Else
$write=StringRegExpReplace($read,'[^\.0-9]+','')
$temp=StringSplit($write,".",1+2)
if UBound($temp)>2 Then $write=$temp&"."&$temp
GUICtrlSetData($Input1,$write)
EndIf
EndIf
EndIf
EndFunc
我晕.这样的事,大家都来帮啊..我的问题咋就没有帮帮啊.. 还有一种情况就是 123.00000 或123000. 也需要判断一下非法! 还有一种情况就是 也需要判断一下非法!
gzh888666 发表于 2011-9-21 01:08 http://www.autoitx.com/images/common/back.gif
123.00000 是合法的~
页:
[1]
2