String/Text Validation |
尚未結案
|
Vashee
初階會員 發表:38 回覆:87 積分:25 註冊:2003-03-31 發送簡訊給我 |
How can i convert a string number into integer for number size validation? I use my case as an example
A 5 digi Unique number is input (14568) begin // To prevent Candidate Number longer than 4 digits and blank input
if (length(Can_No.Text) > 4) or (Can_No.Text = '') then
if (THE CODE I NEED TO ADD HERE)
begin
ShowMessage('Invalid Candidate Number'); // Error Message
end first i validation the string's length and blank input, after that#
I want to convert this string into numbers and validation against these statments: the number must be in range of 10000 to 80000
No A.. Z, a..z, all symbols THis is kinda urgently
Thanks for any help
Plz.
|
skurama
中階會員 發表:88 回覆:127 積分:73 註冊:2002-07-22 發送簡訊給我 |
1. convert a string number into integer:
You can use this function. ----> strtoint()
ex:
int num1;
num1 := strtoint(Edit1.text); There is no error, if the value of Edit1.text is '12345' (it is a numeric).
But the value of Edit1.text is '12a34' then Error. so.... 2. No A.. Z, a..z, all symbols:
You should code this program in the OnKeyPress Event In Edit1. procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if pos(Key, '0123456789') = 0 then Key = #0;
end; If the user types not 0~9 then the key is no value; 3. To check the number in range of 10000 to 80000:
If you have already added onKeyPress Event that likes no.2,
then the value of Edit1 is a numeric.
So you can use the function strtoint() to change from a string of Edit1.text to a integer. ex:
if trim(Edit1.text) <> '' then
if (Strtoint(Edit1.text)) >= 10000) And (Strtoint(Edit1.text) <= 80000) Then Begin
//To do something that you want to do!
End; Then you can ignore this program
'if (length(Can_No.Text) > 4) or (Can_No.Text = '') then' finally....
My english is very poor, thanks for your read. ----------------
快滿一年程設師,
日日工作寫程式,
每買樂透眼框溼,
望能早成系分師。
----------------
------
---------------- 初出芧房程設師, 左鍵右鼠寫程式, 日扣夜寫眼框溼, 望能早成系分師。 ---------------- |
Vashee
初階會員 發表:38 回覆:87 積分:25 註冊:2003-03-31 發送簡訊給我 |
|
timhuang
尊榮會員 發表:78 回覆:1815 積分:1608 註冊:2002-07-15 發送簡訊給我 |
Use error handle with StrToInt to examine if data is number.
procedure TForm1.Button2Click(Sender: TObject); var FData: integer; function IsNumeric(FValue: String): Boolean; begin try StrToInt(FValue); Result := true; except Result := false; end; end; begin if IsNumeric(Edit1.Text) then begin FData := StrToInt(Edit1.Text); if (FData >= 10000) and (FData <= 80000) then ShowMessage('ok') else ShowMessage('invalid data'); end else ShowMessage('invalid data'); end; |
Vashee
初階會員 發表:38 回覆:87 積分:25 註冊:2003-03-31 發送簡訊給我 |
|
Vashee
初階會員 發表:38 回覆:87 積分:25 註冊:2003-03-31 發送簡訊給我 |
Also, I cannot fit this into another procedure.. procedure TStudents.Button4Click(Sender: TObject); begin // To prevent Candidate Number longer than 4 digits and blank input
if (length(Can_No.Text) <> 4) or (Can_No.Text = '') then
begin
ShowMessage('Invalid Candidate Number'); // Error Message
end else // To prevent Centre Number longer than 5 digits and blank input
if (length(Centre_No.Text) <> 5) or (Centre_No.Text = '') then
begin
ShowMessage('Invalid Centre Number');
end {WHERE I PLACE YOUR SUGGEST CODING} else
begin
tStudent.Append;
tStudent.FieldByName('CandidateName').AsString := Can_Name.Text;
tStudent.FieldByName('CandidateNumber').AsString := Can_no.Text;
tStudent.FieldByName('CentreNumber').AsString := Centre_No.Text;
tStudent.FieldByName('CentreName').AsString := Centre_Name.Text;
tStudent.Post;
end; end; The function bit cannot fit into it
|
timhuang
尊榮會員 發表:78 回覆:1815 積分:1608 註冊:2002-07-15 發送簡訊給我 |
Add the red code into your program !!
procedure TStudents.Button4Click(Sender: TObject); function IsNumeric(FValue: String): Boolean; begin try StrToInt(FValue); Result := true; except Result := false; end; end; begin // To prevent Candidate Number longer than 4 digits and blank input if (length(Can_No.Text) <> 4) or (Can_No.Text = '') or (not IsNumeric(Can_No.Text)) then begin ShowMessage('Invalid Candidate Number'); // Error Message end else // To prevent Centre Number longer than 5 digits and blank input if (length(Centre_No.Text) <> 5) or (Centre_No.Text = '') or (not IsNumeric(Centre_No.Text)) then begin ShowMessage('Invalid Centre Number'); end else begin tStudent.Append; tStudent.FieldByName('CandidateName').AsString := Can_Name.Text; tStudent.FieldByName('CandidateNumber').AsString := Can_no.Text; tStudent.FieldByName('CentreNumber').AsString := Centre_No.Text; tStudent.FieldByName('CentreName').AsString := Centre_Name.Text; tStudent.Post; end; end; |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |