找回密码
 加入
搜索
查看: 15125|回复: 28

[AU3基础] 【已解决】Input 如何只能输入正数,可以是小数

 火... [复制链接]
发表于 2011-9-12 22:49:19 | 显示全部楼层 |阅读模式
本帖最后由 touch_xu 于 2011-9-21 20:46 编辑

如题,在论坛找了下,没有结果:
GUICtrlCreateInput("", 79, 122, 80, 31,$ES_NUMBER) 这样的话 0124 这样的数是可以接受的

但是56.5这样的数是不能接受的,我要的所以的正数都可以接受,如何实现,谢谢!
发表于 2011-9-13 00:20:49 | 显示全部楼层
借用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
发表于 2011-9-13 07:07:22 | 显示全部楼层
楼上是使用正则的,学习的了!
 楼主| 发表于 2011-9-19 21:04:07 | 显示全部楼层
借用xayle 的帖子修改给你!
gzh888666 发表于 2011-9-13 00:20



    十分感谢,但是0012这样的数字仍然接开受,以0开头的数是不合法的,如何解决,谢谢
发表于 2011-9-20 12:47:26 | 显示全部楼层
本帖最后由 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或.")
                
        ElseIf  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
 楼主| 发表于 2011-9-20 21:26:59 | 显示全部楼层
回复  touch_xu
gzh888666 发表于 2011-9-20 12:47



    十分感谢,问题已经解决,如果能直接把

0012434 替换为 12434 ,不论开始有多少个0
... 123434 替换为 替换为,不论开始有多少个.

其它非法字符换为空白,就更加完美了,再次感谢
发表于 2011-9-20 21:55:24 | 显示全部楼层
本帖最后由 tsui 于 2011-9-20 21:59 编辑

其实上面的reg是不严谨的,可以多个点
 楼主| 发表于 2011-9-20 22:42:25 | 显示全部楼层
其实上面的reg是不严谨的,可以多个点
tsui 发表于 2011-9-20 21:55



    所以我要的就是一个合法的正数,当然不要
111...333
111.3333.4444
也不要负数了,继续求教。
发表于 2011-9-20 23:18:19 | 显示全部楼层
本帖最后由 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, "[0.]+?([1-9].+)", 1)
                        GUICtrlSetData($Inputuser, $one2[0])
                EndIf
                        GUICtrlSetData($Label, "√ 输入正确.")
                        GUICtrlSetFont($Label, 9, 800, 0, "Tahoma")
                        GUICtrlSetColor($Label, 0x008000)
                        GUICtrlSetFont($Label, 9, 400, 0, "Tahoma")
                EndIf
        EndFunc   ;==>InputCheck
这个就可以自动删除开头的“0.”,多个点的不知道你需要不,没写!如果需要判断一下.的数量为<=1就行,很简单!
发表于 2011-9-20 23:19:26 | 显示全部楼层
本帖最后由 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), '^[1-9]\d*(?:\.\d+)?$|^0\.\d+$|^0$') Then
                GUICtrlSetData($Label, '√ 输入正确.')
        Else
                GUICtrlSetData($Label, '× 输入错误.')
        EndIf
EndFunc   ;==>InputCheck
发表于 2011-9-20 23:23:58 | 显示全部楼层
本帖最后由 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或.")
                
        If  Not 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大佬的帖子让我受益匪浅啊,非常非常感谢喽,嘿嘿
发表于 2011-9-20 23:33:51 | 显示全部楼层
#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[0]&"."&$temp[1]
                                GUICtrlSetData($Input1,$write)
                        EndIf
                EndIf
        EndIf
EndFunc
发表于 2011-9-21 00:41:26 | 显示全部楼层
我晕.这样的事,大家都来帮啊..我的问题咋就没有帮帮啊..
发表于 2011-9-21 01:08:44 | 显示全部楼层
还有一种情况就是
123.00000 或123000.
也需要判断一下非法!
发表于 2011-9-21 01:44:55 | 显示全部楼层
还有一种情况就是 也需要判断一下非法!
gzh888666 发表于 2011-9-21 01:08



    123.00000 是合法的~
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-16 23:44 , Processed in 0.083204 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表