How to prevent input of chr in input field!

Results 1 to 9 of 9

Thread: How to prevent input of chr in input field!

  1. #1
    Martin, Sweden Guest

    Default How to prevent input of chr in input field!

    Howdy freaks!<BR>I would be glad if someone could help me with a small problem!<BR>I have form input fields where people supposed to input an integer, and I would like to make it impossible to input something else then an integer!<BR>Am I able to convert all input chars to integers, or maybee better prevent them from passing thru the form at all??<BR><BR>Would be nice with some help!

  2. #2
    Joseph Hatcher Guest

    Default RE: How to prevent input of chr in input field!

    Basically check the field when the user either pushes a button or the field loses focus. If there is a character in there then send them a notice.<BR><BR>If you are using vbscript you could use<BR>IsNumeric(document.form.field.value)<BR><BR >But remember that this will return true if the number is a float. Such as 3.45 or 123.45<BR><BR>If you are using javascript the following code should work?<BR><BR> <BR>function IsInteger(num)<BR>{<BR> answer=new Boolean(true)<BR> i=0;<BR> if (i==num.length)<BR> {<BR> answer=false<BR> }<BR> while (i&#060;num.length && answer)<BR> { <BR> testval=num.charAt(i);<BR> if (testval!="0" && testval!="1" && testval!="2" && testval!="3" && testval!="4" && testval!="5" && testval!="6" && testval!="7" && testval!="8" && testval!="9")<BR> {<BR> answer=false;<BR> } <BR> i++;<BR> }<BR> return(answer);<BR>}<BR><BR>The only thing is becareful cause it will send back a false if the string is a blank string so check for this condition also.<BR><BR>If this does not answer your question or you would like more detailed explanation of something please ask :)<BR><BR>Hope this helps.

  3. #3
    Martin (Sweden) Guest

    Default RE: How to prevent input of chr in input field!

    Man thanks for your advise!<BR>I´ve tried solve it, but I don´t seem to get it right!<BR>I´m using VBScript, wich is a new experience for me.<BR><BR>My form that I´ve don´t want anything else then integers look like this!<BR><BR>&#060;form action="korg.asp" method="post" target="korg"><BR>&#060;input name="fquantity" value="1" size="2" maxlength="2"><BR>&#060;input name="order" type="submit" value="click"><BR>&#060;/form><BR><BR>I´ve tried your advise with:<BR>IsNumeric(document.form.field.value)<BR>< BR>But I´ve put it wrong I think!<BR><BR>Please help me<BR>I would like to have a example with my form, and a error mess, if it´s not an integer! Use VBScript!<BR><BR>Thanks Pal

  4. #4
    Join Date
    Dec 1969
    Posts
    9

    Default RE: How to prevent input of chr in input field!

    Hi, I was reading about your problem and thought I could help. This should do the trick for you.<BR><BR>&#060;HTML><BR>&#060;SCRIPT LANGUAGE="VBScript"><BR><BR>Dim strOldVal<BR><BR>Sub CheckNumeric(objFld)<BR> if not IsNumeric(objFld.value) OR InStr(objFld.value,".") OR InStr(objFld.value,"$") OR InStr(objFld.value,",") then<BR> msgbox "Value must be an integer! Please Re-enter value."<BR> objFld.value = strOldVal<BR> end if<BR>end sub<BR><BR>sub SaveEntry(objFld)<BR> strOldVal = objFld.value<BR>end sub<BR>&#060;/SCRIPT><BR>&#060;BODY><BR>Enter Integer Value:&#060;INPUT TYPE="TEXT" NAME="txtIntValue" onFocus="SaveEntry(txtIntValue)" onBlur="CheckNumeric(txtIntValue)"><BR>&#060;/BODY><BR>&#060;/HTML>


  5. #5
    Joseph Hatcher Guest

    Default RE: How to prevent input of chr in input field!

    &nbsp;<BR>&#060;script Language="VBScript"><BR>Sub order_onclick()<BR>if (IsNumeric(document.korg.fquantity.value)) then<BR>document.korg.submit()<BR>else<BR>alert "The quantity must be a number"<BR>end if<BR>End sub<BR>&#060;/script><BR><BR>&#060;form name="korg" action="korg.asp" method="post" target="korg"><BR>&#060;input name="fquantity" value="1" size="2" maxlength="2"><BR>&#060;input name="order" type="button" value="click"><BR>&#060;/form><BR><BR>This should work. You might have to tweak it a little I have not tested it. <BR><BR>Please keep in mind that isnumeric will allow decimal places so this is not a true integer check. If you want to do that I think you will have to write your own code. If you would like help with that just let me know.<BR><BR>Also this code will not work in Netscape, only in IE. If you want help with this let me know :)<BR><BR>Good Luck<BR><BR>Joseph Hatcher<BR><BR>

  6. #6
    Martin, Sweden Guest

    Default RE: How to prevent input of chr in input field!

    Thank you guys!<BR>Really kind of ya!<BR>But the problem still remains because I want it to work with both explorer and netscape.<BR>And it would be nice if it was handle on the server side, so user with javascript/vbscript disabled browsers can take part of it to!<BR><BR>Thanks<BR>You guys rules

  7. #7
    Join Date
    Dec 1969
    Posts
    9

    Default RE: How to prevent input of chr in input field!

    Try this one. No client-side script, all server side validation.<BR><BR>&#060;HTML><BR>&#060;BODY><BR>& #060;% <BR>if Request.Form("MODE")="VALIDATE" then<BR> strCheckValue = Request.Form("txtIntValue")<BR> if not IsNumeric(strCheckValue) OR InStr(strCheckValue,".") OR InStr(strCheckValue,"$") OR InStr(strCheckValue,",") then &#037;&#062;<BR><BR>&#060;FORM NAME="myForm" ACTION="default.asp" METHOD="POST"><BR>Enter Integer Value:&#060;INPUT TYPE="TEXT" NAME="txtIntValue">&#060;INPUT TYPE="HIDDEN" NAME="MODE" VALUE="VALIDATE">&#060;FONT COLOR=#FF0000> INVALID VALUE - PLEASE RE-ENTER<BR>&#060;INPUT TYPE="SUBMIT" NAME="btnSubmit" VALUE="SUBMIT"><BR>&#060;/FORM><BR> &#060;% else &#037;&#062;<BR> Your entry has been validated <BR><BR> &#060;% end if &#037;&#062;<BR>&#060;% else &#037;&#062;<BR>&#060;FORM NAME="myForm" ACTION="default.asp" METHOD="POST"><BR>Enter Integer Value:&#060;INPUT TYPE="TEXT" NAME="txtIntValue">&#060;INPUT TYPE="HIDDEN" NAME="MODE" VALUE="VALIDATE"><BR>&#060;INPUT TYPE="SUBMIT" NAME="btnSubmit" VALUE="SUBMIT"><BR>&#060;/FORM><BR>&#060;% end if &#037;&#062;<BR>&#060;/BODY><BR>&#060;/HTML>

  8. #8
    Joseph Hatcher Guest

    Default RE: How to prevent input of chr in input field!

    This will also work with either browser<BR><BR>&#060;script language=javascript><BR>function IsInteger(num)<BR>{<BR>answer=new Boolean(true)<BR>i=0;<BR>if (i==num.length)<BR>{<BR>answer=false<BR>}<BR>while (i&#060;num.length && answer)<BR>{ <BR>testval=num.charAt(i);<BR>if (testval!="0" && testval!="1" && testval!="2" && testval!="3" && testval!="4" && testval!="5" && testval!="6" && testval!="7" && testval!="8" && testval!="9")<BR>{<BR>answer=false;<BR>} <BR>i++;<BR>}<BR>return(answer);<BR>}<BR><BR>funct ion check_val()<BR>{<BR>if (!IsInteger(document.korg.order.value))<BR>{<BR>al ert("This value has to be a number")<BR>}<BR>else<BR>{<BR>document.korg.submit ()<BR>}<BR>}<BR>&#060;/script><BR><BR><BR><BR>&#060;form name="korg" action="korg.asp" method="post" target="korg"><BR>&#060;input name="fquantity" value="1" size="2" maxlength="2"><BR>&#060;input name="order" type="button" value="click" onclick="check_val()"><BR>&#060;/form>

  9. #9
    phanindra.t Guest

    Default RE: How to prevent input of chr in input field!

    here is an asp script that you can use:<BR><BR>for i= 1 to len(Request.Form("text"))<BR>if (mid(Request.Form("text"),i,1)&#060;"0")or (mid(Request.Form("text"),i,1)>"9") then<BR>if mid(Request.Form("text"),i,1)&#060;>"." then<BR>Response.Write("&#060;b>&#060;i>Characters other than &#039.&#039 are not allowed,again fill details of text Field<BR>&#060;/b>&#060;i>")<BR>exit for<BR>end if<BR>end if <BR><BR>here is javascript you can use:<BR>function numcheck()<BR>{<BR>var len=doument.form.val.length<BR>for(var i=0;i&#060;len;i++)<BR>{<BR>if(doument.form.val.su bstring(i,i+1)&#060;"0" &#124&#124doument.form.val.substring(i,i+1)>"9")<B R>{<BR>if(doument.form.val.substring(i,i+1)!=".")< BR>{<BR>alert("CHARACTERS NOT ALLOWED")<BR> this.appform.elements[n].value=""<BR> this.appform.elements[n].focus()<BR>break;<BR>}<BR>}<BR>}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •