Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6251829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:41:10+00:00 2026-05-24T13:41:10+00:00

I would like to change the value of an input field when the form

  • 0

I would like to change the value of an input field when the form is submitted depending on it’s content.

Currently I have this and expect I am a long way from what I actually need, can’t seem to find the info I am after to make it all work together.

Any help appreciated.

<script type="text/javascript">
function vet()
{
    if(this.abc.value.indexOf("CW-") >= 0)
    {
    return true;
    }
    else
    {
    this.abc.value = 'CW-' + this.abc.value; return true;
    }
}
</script>

<form name="simple1" method="get" onsubmit="vet()" action="simple.php">
<input class="saip1" name="abc" type="text" value="Input Value" onfocus="this.value='';"/>

Basically I want to check that the user has included “CW-” (or indeed any case variant of that cw- cW- Cw-) at the beginning of their value. If they have just return the value as it is. If not I would like to add CW-.

After some reading I tried the indexOf approach but I don’t really understand javascript and am sure I have done something wrong.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:41:11+00:00Added an answer on May 24, 2026 at 1:41 pm

    Your approach is fine but you were making it a little complicated.

    Also, it’s good practice to use onsubmit etc. through JavaScript instead of through HTML. You could also do this for onfocus.

    http://jsfiddle.net/PTspF/

    HTML:

    <form id="simple1" method="get" action="simple.php">
    
        <input class="saip1" name="abc" type="text" value="Input Value" onfocus="this.value='';"/>
    
    </form>
    

    JavaScript:

    You could check for an indexOf of 0 as you wish “CW-” to appear at the beginning of the string. The toUpperCase() makes cw- CW- so that variations also are found correct.

    window.onload=function() {
      document.getElementById("simple1").onsubmit=function() {
        if (this.abc.value.toUpperCase().indexOf("CW-") !== 0) {
          this.abc.value = 'CW-' + this.abc.value; // prepend if not present
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a input field like below, <input name=update_price type=text id=update_price value=£ size=8 maxlength=8
I would like to change my default route values. Right now, I have: routes.MapRoute(
I have a ListView and each item have a TextView. I would like change
A form has a input field which would contain text values . There is
I have a form which has a field which takes in a hex value
I have a form which is submitted remotely when the various elements change. On
Using Javascript, I would like change the background color of the numbers in an
I would like to change the background color of the row (to red) when
I would like to change the default style for vector features in a map
I would like to change all captions from one type to another. While not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.