線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1299
推到 Plurk!
推到 Facebook!

String/Text Validation

尚未結案
Vashee
初階會員


發表:38
回覆:87
積分:25
註冊:2003-03-31

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-05-11 22:36:05 IP:217.36.xxx.xxx 未訂閱
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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-05-11 23:30:10 IP:61.13.xxx.xxx 未訂閱
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

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-05-12 00:20:14 IP:217.36.xxx.xxx 未訂閱
your english really is a little hard to understand.. and i am a little confuse too do u mind putting the code together for me? how should the code look like in complete form with all the validation statement included? thanks
timhuang
尊榮會員


發表:78
回覆:1815
積分:1608
註冊:2002-07-15

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-05-12 01:23:30 IP:61.221.xxx.xxx 未訂閱
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

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-05-12 03:34:59 IP:217.35.xxx.xxx 未訂閱
function IsNumeric(FValue: String): Boolean; begin try StrToInt(FValue); Result := true; except Result := false; end; end; Is this one standalone?
Vashee
初階會員


發表:38
回覆:87
積分:25
註冊:2003-03-31

發送簡訊給我
#6 引用回覆 回覆 發表時間:2003-05-12 03:56:39 IP:217.35.xxx.xxx 未訂閱
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

發送簡訊給我
#7 引用回覆 回覆 發表時間:2003-05-12 13:12:17 IP:211.76.xxx.xxx 未訂閱
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;
系統時間:2024-07-03 1:31:21
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!