2013-05-02 119 views
3

我使用谷歌maps.my UI創建地址驗證的Web應用程序:地址驗證

] enter image description here

現在,我想是按OK按鈕地址後,應顯示到你的地址查找像這樣701字段,Sunnywale,CA 94089 現在我的確定按鈕單擊事件是

FullAddress.Text = AddressLine1.Text +','+''+ City.Text +','+''+ State .Text +''+ Zip.Text;

但是當我添加JavaScript來調用谷歌地圖與我的程序它不工作其throgh像樣本圖片的錯誤。請幫助我們。 OK按鈕:

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="OK" 
      Width="62px" style="margin-left: 0px" /> 

驗證&找到Me按鈕代碼:

<asp:Button ID="Submit" runat="server" style="margin-left: 97px" Text="Validate &amp; Locate Me" Width="137px" /> 

現在jQuery和地理編碼部分:

<script type="text/javascript"> 
    // The following code show execute only after the page is fully loaded 
    $(document).ready(function() { 
     if ($('#MyForm').exists()) { 

      // Enable jQuery Validation for the form 
      $("#MyForm").validate({ onkeyup: false }); 

      // Add validation rules to the FullAddress field 
      $("#FullAddress").rules("add", { 
       fulladdress: true, 
       required: true, 
       messages: { 
        fulladdress: "Google cannot locate this address." 
       } 
      }); 

      // This function will be executed when the form is submitted 
      function FormSubmit() { 
       $.submitForm = true; 
       if (!$('#MyForm').valid()) { 
        return false; 
       } else { 
        if ($("#FullAddress").data("IsChecking") == true) { 
         $("#FullAddress").data("SubmitForm", true); 
         return false; 
        } 

        alert('Form Valid! Submit!'); 
        // return true; // Uncomment to submit the form. 
        return false;  // Supress the form submission for test purpose. 

       } 
      } 

      // Attach the FormSubmit function to the Submit button 
      if ($('#Submit').exists()) { 
       $("#Submit").click(FormSubmit); 
      } 

      // Execute the ForumSubmit function when the form is submitted 
      $('#MyForm').submit(FormSubmit); 
     } 
    }); 

    // Create a jQuery exists method 
    jQuery.fn.exists = function() { return jQuery(this).length > 0; } 

    // Position the Google Map 
    function Map(elementId, geolocation) { 
     var myOptions = { 
      zoom: 13, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     } 
     var map = new google.maps.Map(document.getElementById(elementId), myOptions); 
     map.setCenter(geolocation); 
    } 

    // FullAddress jQuery Validator 
    function FullAddressValidator(value, element, paras) { 

     // Convert the value variable into something a bit more descriptive 
     var CurrentAddress = value; 

     // If the address is blank, then this is for the required validator to deal with. 
     if (value.length == 0) { 
      return true; 
     } 

     // If we've already validated this address, then just return the previous result 
     if ($(element).data("LastAddressValidated") == CurrentAddress) { 
      return $(element).data("IsValid"); 
     } 

     // We have a new address to validate, set the IsChecking flag to true and set the LastAddressValidated to the CurrentAddress 
     $(element).data("IsChecking", true); 
     $(element).data("LastAddressValidated", CurrentAddress); 

     // Google Maps doesn't like line-breaks, remove them 
     CurrentAddress = CurrentAddress.replace(/\n/g, ""); 

     // Create a new Google geocoder 
     var geocoder = new google.maps.Geocoder(); 
     geocoder.geocode({ 'address': CurrentAddress }, function (results, status) { 

      // The code below only gets run after a successful Google service call has completed. 
      // Because this is an asynchronous call, the validator has already returned a 'true' result 
      // to supress an error message and then cancelled the form submission. The code below 
      // needs to fetch the true validation from the Google service and then re-execute the 
      // jQuery form validator to display the error message. Futhermore, if the form was 
      // being submitted, the code below needs to resume that submit. 

      // Google reported a valid geocoded address 
      if (status == google.maps.GeocoderStatus.OK) { 

       // Get the formatted Google result 
       var address = results[0].formatted_address; 

       // Count the commas in the fomatted address. 
       // This doesn't look great, but it helps us understand how specific the geocoded address 
       // is. For example, "CA" will geocde to "California, USA". 
       numCommas = address.match(/,/g).length; 

       // A full street address will have at least 3 commas. Alternate techniques involve 
       // fetching the address_components returned by Google Maps. That code looks even more ugly. 
       if (numCommas >= 3) { 

        // Replace the first comma found with a line-break 
        address = address.replace(/, /, "\n"); 

        // Remove USA from the address (remove this, if this is important to you) 
        address = address.replace(/, USA$/, ""); 

        // Check for the map_canvas, if it exists then position the Google Map 
        if ($("#map_canvas").exists()) { 
         $("#map_canvas").show(); 
         Map("map_canvas", results[0].geometry.location); 
        } 

        // Set the textarea value to the geocoded address 
        $(element).val(address); 

        // Cache this latest result 
        $(element).data("LastAddressValidated", address); 

        // We have a valid geocoded address 
        $(element).data("IsValid", true); 
       } else { 
        // Google Maps was able to geocode the address, but it wasn't specific 
        // enough (not enough commas) to be a valid street address. 
        $(element).data("IsValid", false); 
       } 

       // Otherwise the address is invalid 
      } else { 
       $(element).data("IsValid", false); 
      } 

      // We're no longer in the midst of validating 
      $(element).data("IsChecking", false); 

      // Get the parent form element for this address field 
      var form = $(element).parents('form:first'); 

      // This code is being run after the validation for this field, 
      // if the form was being submitted before this validtor was 
      // called then we need to re-submit the form. 
      if ($(element).data("SubmitForm") == true) { 
       form.submit(); 
      } else { 
       // Re-validate this property so we can return the result. 
       form.validate().element(element); 
      } 
     }); 

     // The FullAddress validator always returns 'true' when initially called. 
     // The true result will be return later by the geocode function (above) 
     return true; 
    } 

    // Define a new jQuery Validator method 
    $.validator.addMethod("fulladdress", FullAddressValidator); 
</script> 

    </body> 
    </html> 

壓制後請幫忙帶進OK按鈕詳細地址心不是你地址看起來像字段。而不是它通過一條消息:此字段是必需的。如果需要更多詳細信息,請提及它

回答

0

只需使用2個表格即可保留您的控件。第一個將有地址欄和OK按鈕。
其餘的會休息。相應地更改您的代碼。它應該工作。否則,您需要分配類並將控件分組爲2組。
我的意思是2套是「確定」按鈕應驗證上面的字段。和「驗證和定位我」按鈕應該只驗證「你的地址看起來像」文本框。有了jQuery,你可以想出很多方法來分組控制和驗證它們。

如果不解決,請提供HTML以及或使用http://jsfiddle.net/

- 編輯 -
當您按下OK,你的問題是,它證明了它下面的字段。現在我覺得你已經使用了一個沒有任何分組的必填字段驗證器。請指定一個ValidationGroup屬性來分隔「確定」按鈕和「驗證並定位我」按鈕的驗證條件。 如果你不知道如何使用ValidationGroup,請告訴我。

〜CJ