473,659 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to do I validate a textarea in a form in javascript

17 New Member
Expand|Select|Wrap|Line Numbers
  1. <script>
  2.     function val()
  3.     {
  4.       //ifnewline found or blank raise an alert
  5.     } 
  6.    </script>
  7.    <form>  
  8.    <textarea name = "pt_text" rows = "8" cols = "8" class = "input" WRAP ></textarea>
  9.    <input type=""button" onclick="val();"
  10.     </form>
Sep 6 '19 #1
3 3969
gits
5,390 Recognized Expert Moderator Expert
what exactly should it validate for? you can use the

indexOf-method

or

Regular Expression

for that purpose.
Sep 6 '19 #2
AjayGohil
83 New Member
try this script for textarea validation
Expand|Select|Wrap|Line Numbers
  1.  <form>  
  2.    <textarea name = "pt_text" rows = "8" cols = "8" class = "details" WRAP ></textarea>
  3.    <input type=""button" onclick="validate();"
  4.     </form>

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function validate()
  3. {
  4.      var obj1 = document.getElementById('details');
  5.          if(trimTextarea(obj1.value) == '') 
  6.          {      
  7.               alert("Please Provide Details!");
  8.               obj1.focus();
  9.               return false;       
  10.          }
  11. }
  12.  
  13. function trimTextarea(str) 
  14.     return str.replace(/^\s+|\s+$/g,''); 
  15. }
  16. </script>
Nov 5 '19 #3
gits
5,390 Recognized Expert Moderator Expert
well - the OP didn't specify for what it should validate - so throwing out such code is just guesswork. besides that a much easier and much more performant way to test for an empty value in that field would make use of Javascript's typeconversion system. You would only need to check:

Expand|Select|Wrap|Line Numbers
  1. value == 0
this 'autocasts' the value to a number and it does exactly what your trimfunction does - but with much less expensive operations.

PS: you just cant put in only 0(s) here - but usually that is ok as well with the app logic.
Nov 5 '19 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1953
by: Techy | last post by:
I have two fields in my form so called : invoice and cash Now I want to validate this form on the client side with the help of javascript in such a way that if one of these fields is empy an alert box should pop up notifying the client. Now if one is empty it should let the form to process thanks
1
12218
by: CiscoGuy | last post by:
Is it possible to read each line from a HTML textarea into a javascript array. For example: If I were to input into the textarea: 1 4 6 3 Could I somehow have javascript read it so: array=1 array=4
1
2130
by: snipersix | last post by:
How do you display values in a textarea using javascript. ex. i have 2 radio buttons o Adult =$9.99 o Child =$6.99 i need it to be, so that if i select adult in the radio button option , it will display 9.99 in my textarea at the other frame. Cause im using 2 frames for this html file. The radio buttons are at the left frame and the textarea is at the right frame.
3
4829
by: bravo | last post by:
hi i wish to set output of a jsp page having tabular data along with colors and fonts into the textarea using javascript.... is it possible via javascript???? if yes how?// any alternative....
1
2463
by: saurabhswati722 | last post by:
My Urgent question : How to validate textfield using Javascript in PHP?
6
6992
by: RodPhp5 | last post by:
Please i need help on the code i have posted below. I am supposed to validate the form before posting the data into the database. When i run the code on my computer it is submiting all the data into the database, but the password is not submited and before submiting the data for any missing fields the user must be notified. That is what i am trying to do with the if statements. Pleae help its urgent <html> <head> <title>Insert...
2
1490
by: impin | last post by:
how to validate this form. when user submits without selecting the options, must get a alert. my code echo "<form method='post' id='submit' action='checkresult.php'>"; $sql="SELECT * FROM cquestions where showdate='$today' limit 2"; $result=mysql_query($sql); while ($row = mysql_fetch_array($result)) { echo "<p>" . $row . "</p>"; $sql2="SELECT * FROM canswers where cqid=".$row;
3
1523
by: komathi043 | last post by:
i need to validate a form using javascript.But it should not validate with mssbox.i need different method for displaying err msg
0
1100
by: Yousef Altaf | last post by:
I am trying to validate my form fields and redirect the user to success page so this is the PHP code if (empty($_POST)) { $error = "Experiences Required"; } else { $experiences = check_input($_POST); if (!preg_match("/^*$/", $experiences)) { $error = "Only letters, numbers and '_' allowed";
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8523
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.