|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
User Input Validation Problem
I am trying to do something that prevent user submit blank data to my database.
Here is my code: strSubject = upload.Form("subject") strContent = upload.Form("comment") If (strSubject="") Then Response.Redirect "http.....asp" Else If (strContent="") Then Response.Redirect "http:...asp" End If End If When only the content is blank and subject line has text. This validation doesn't work. It allow ppl enter empty data to content. If the subject leaves blank, then this validation works. What is wrong? |
|
#2
|
||||
|
||||
|
Try something like this
Code:
If Len(strSubject) = 0 Then
Response.Redirect("http://...asp")
End If
If Len(strContent) = 0 Then
Response.Redirect("http://...asp")
End If
|
|
#3
|
|||
|
|||
|
Quote:
It's samething. Not working. I was wondering if I can create a function to check the user input. If it is blank, then prompt user an error message at client? I'm using this as my prefilled text field. It will be stored in the database again. I do not want ppl delete everything and submit a empty data to the table. <INPUT STYLE="width: 715px" TYPE="Text" NAME="subject" VALUE="<%Response.Write objRS("Subject")%>" SIZE="20" MAXLENGTH="75"> How can I modify this to validate the text field before submission? Thanks. |
|
#4
|
|||
|
|||
|
I would definitely recommend doing your validation on the client. Attach it to your submit button with something like:
<input type=button onClick="ValidateInput()" value="Submit"> <script> If(form1.subject=="") { alert("Please enter subject."); } else { form1.submit } </script> |
|
#5
|
|||
|
|||
|
Quote:
How about validating both subject and comment field? How should I modify this function to make it work? Thanks. |
|
#6
|
|||
|
|||
|
Quote:
My button is: <INPUT TYPE="SUBMIT" NAME="send" VALUE="UPDATE" Class="button"> How can I modify it to work with your function? |
|
#7
|
||||
|
||||
|
Here is a way in which you can use some of the ideas shared above.
Code:
<html>
<head>
<script language="JavaScript">
function validate()
{
if (document.main.subject.value=="")
{
alert("Subject Must contain a value");
return false;
}
else
{
if (document.main.comment.value == "")
{
alert("Comment Must contain a Value");
return false;
}
else
{ return true; }
}
}
</script>
</head>
<body>
<form name="main" action="" onSubmit="return validate();">
<input type="text" name="subject" size="20">
<br>
<input type="text" name="comment" size="20">
<br>
<input type="submit" value="Update">
</form>
</body>
</html>
Test it, it should give you what you want, make sure that you put an action on the <form> tag. Post back if you have any questions |
|
#8
|
|||
|
|||
|
Quote:
Sorry, I sent you some incomplete code last time. To validate other fields you just put an else if statement: Code:
<SCRIPT LANGUAGE=javascript>
<!--
function ValidateInput()
{
if(form1.subject.value=="")
{
alert("Please enter subject.");
}
else if(form1.comment.value=="")
{
alert("Please enter comment.");
}
else
{
form1.submit
}
}
//-->
</SCRIPT>
You should change your button type from "SUBMIT" to "BUTTON" because you want the code to validate before you submit. Yes! The one above is much cleaner. Go with that. ![]() |
|
#9
|
|||
|
|||
|
Form Action Question
In the action property, I have the URL of ASP code page that will process this form submission.
So since I need to put onsubmit validate(), where do I put that URL? My form looks like this: <FORM NAME="frmSend" ACTION="http://.......asp" METHOD="POST"> |
|
#10
|
|||
|
|||
|
Ok,, I see what I have did wrong. Never mind.
Thanks. |
|
#11
|
|||
|
|||
|
It's not working....
I do not know why this is not work. I have tried all kinds of user input validation methods. None of them works.
Here is the story. Currently I am working on user input validation on Modify function. When user clicks on Modify button, it will allow them modify their posts. In order to prevent them submitting empty data to the database. I need to add some type of user input validation. Here is the code: ======================= <script language="JavaScript"> function validate() { if (document.ModifyMessageSend.subject.value=="") { alert("Subject Must contain a value"); return false; } else { if (document.ModifyMessageSend.comment.value == "") { alert("Comment Must contain a Value"); return false; } else { return true; } } } </script> ============================= The code of the form: ============================= <FORM NAME="ModifyMessageSend" ACTION="http://...ProcessSubmission.asp" onSubmit="return validate();" METHOD="POST"> <TABLE BORDER=0> <TR> <TD><INPUT STYLE="width: 715px" TYPE="Text" NAME="subject" VALUE="<%Response.Write strSubject%>" SIZE="20" MAXLENGTH="75"></TD> </TR> <TR> <TD><TEXTAREA NAME = "comment" ROWS = "13" COLS = "87"> <%Response.Write strContent%></TEXTAREA> </TD> </TR> <%If objRS("filestat")="1" Then%> <tr valign=center> <td><span class="mytext">Remove attached photo? <INPUT TYPE="radio" NAME="FileStat" Value="yes">Yes <INPUT TYPE="radio" NAME="FileStat" Value="no">No </span></td> </tr> <%Else%> <tr> <td> <A HREF="http://.....asp?qid=<%=strThreadID%>"> <img src="http://....Attachbutton.jpg" width="75" height="25" BORDER="0" align=left> </A> </td> </tr> <tr> <td> <hr align=left> </td> </tr> <%End If%> <TR> <TD align=center> <INPUT TYPE="SUBMIT" NAME="send" VALUE="UPDATE" Class="button"> </TD> </TR> </TABLE> </FORM> ===================================== No matter what I try. It still allow user enter empty value to the table. What seems to be wrong here? ![]() Last edited by Discusman : February 2nd, 2005 at 11:13 AM. |
|
#12
|
|||
|
|||
|
User Validation Update...
I have created a new ASP page with the same exact form that I listed in my previous post.
It works fine with the validate() function when I removed all the database connection on the page. It stops working after I add the database connection to the page. This is User Post Modify page, so the "Subject" and "Comment" fields are prefilled with data that retrieved from the database. I simply use the response.write strSubject and strComment to prefill the text input fields. Now it seems preventing the validation from working properly. What is my doing wrong? Thanks for the help. |
|
#13
|
|||
|
|||
|
It doesn't work with Textarea Field
<script language="JavaScript">
function validate() { if (document.ModifyMessageSend.comment.value=="") { alert("Comment Must contain a value"); return false; } else { if (document.ModifyMessageSend.subject.value == "") { alert("Subject Must contain a Value"); return false; } else { return true; } } } </script> <TEXTAREA NAME="comment" ROWS = "13" COLS = "87"> <%'Response.Write strContent%></TEXTAREA> <INPUT TYPE="SUBMIT" VALUE="UPDATE"> <FORM NAME="ModifyMessageSend" ACTION="http://.....asp" onSubmit="return validate();" METHOD="POST"> <TEXTAREA NAME="comment" ROWS = "13" COLS = "87"> <%'Response.Write strContent%></TEXTAREA> <INPUT TYPE="SUBMIT" VALUE="UPDATE"> </FORM> The javascript doesn't validate the text area. |
|
#14
|
||||
|
||||
|
Adding database fields don't matter if your form is correct.
If you are using textarea use something like this: Code:
<html>
<head>
<script language="JavaScript">
function validate()
{
if (document.main.subject.value=="")
{
alert("Subject Must contain a value");
return false;
}
else
{
if (document.main.comment.value == "")
{
alert("Comment Must contain a Value");
return false;
}
else
{ return true; }
}
}
</script>
</head>
<body>
<form name="main" action="" onSubmit="return validate();">
<input type="text" name="subject" size="20">
<br>
<TEXTAREA NAME="comment" ROWS = "13" COLS = "87"></textarea>
<br>
<input type="submit" value="Update">
</form>
</body>
</html>
I tested it and it works. If it doesn't work, post all your relevant code. Maybe you aren't closing a tag somewher. |